无法将数组返回主方法

时间:2018-11-15 04:05:45

标签: java arrays

我正在尝试从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;
   }
}

1 个答案:

答案 0 :(得分: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;
   }
}