无法将数组返回到main方法

问题描述 投票:0回答:1

我试图从wordCheck方法返回intArray并且错误找不到符号 - 当我尝试从数组中打印值时,变量intArray出现在第10,11行和第12行中。我想知道我是否声明方法错误或没有使用正确的返回,或者我可能使我的阵列很差。该数组还应该扫描文本文档以检查单词出现的次数,并且想知道我的循环是否写入它实际上将返回正确的值。

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class Assignment3
{
public static void main(String[] args)
throws FileNotFoundException {
    Scanner f = new Scanner( new File("README.txt"));
    int[] intArray = punctuation(f);
    System.out.println("The word the appears: "+ intArray[0] +" times");
    System.out.println("The word be appears: "+ intArray[1] +" times");
    System.out.println("The word to appears: "+ intArray[2] +" times");
}
public static int[] punctuation(Scanner f){
    ArrayList<String> tokens = new ArrayList();
    while (f.hasNext()){
        String currentToken = f.next();
        currentToken = currentToken.replace("-","")
        .replace("?","")
        .replace("!","")
        .replace(",","")
        .replace(".","")
        .replace(":","")
        .replace(";","")
        .replace("\'","")
        .replace("\"","");
        if (currentToken.length()>0){
            currentToken = currentToken.toLowerCase();
            tokens.add(currentToken);
        }
    }
    return  wordCheck(tokens, f);
}
public static int[] wordCheck(ArrayList<String> tokens, Scanner f){
    int word1Count = 0;
    int word2Count = 0;
    int word3Count = 0;
    while(f.hasNext()){
        boolean word1 = tokens.contains("the");
        if(word1 == true){
            word1Count++;
        }
        boolean word2 = tokens.contains("be");
        if(word2 == true){
            word2Count++;
        }
        boolean word3 = tokens.contains("to");
        if(word3 == true){
            word3Count++;
        }
    }
    int intArray[];
    intArray = new int[3];
    intArray[0] = word1Count;
    intArray[1] = word2Count;
    intArray[2] = word3Count;
    return intArray;
   }
}
java arrays
1个回答
0
投票

你不是从main方法调用puntuation()方法,而是从wordCheck()方法调用puntuation()方法

public class Assignment3
{
public static void main(String[] args)
throws FileNotFoundException {
    Scanner f = new Scanner( new File("README.txt"));
    int[] intArray = punctuation(f);
    System.out.println("The word the appears: "+ intArray[0] +" times");
    System.out.println("The word be appears: "+ intArray[1] +" times");
    System.out.println("The word to appears: "+ intArray[2] +" times");
}
public static int[] punctuation(Scanner f){
    ArrayList<String> tokens = new ArrayList();
    while (f.hasNext()){
        String currentToken = f.nextLine();
        currentToken = currentToken.replace("-","")
        .replace("?","")
        .replace("!","")
        .replace(",","")
        .replace(".","")
        .replace(":","")
        .replace(";","")
        .replace("\'","")
        .replace("\"","");
        if (currentToken.length()>0){
            currentToken = currentToken.toLowerCase();
            tokens.add(currentToken);
        }

    }
    return  wordCheck(tokens, f);
}
public static int[] wordCheck(ArrayList<String> tokens, Scanner f){
int word1Count = 0;
int word2Count = 0;
int word3Count = 0;
for(String s : tokens){
    boolean word1 = s.contains("the");
    if(word1 == true){
        word1Count++;
    }
    boolean word2 = s.contains("be");
    if(word2 == true){
        word2Count++;
    }
    boolean word3 = s.contains("to");
    if(word3 == true){
        word3Count++;
    }
}
int intArray[];
intArray = new int[3];
intArray[0] = word1Count;
intArray[1] = word2Count;
intArray[2] = word3Count;
return intArray;
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.