Cannot return array to main method

ic0n

New Member
#1
Im trying to return intArray from the wordCheck method and the error cannot find symbol - variable intArray is appearing in lines 10,11, and 12 when I try to print the values from the array. I was wondering if I am declaring the method wrong or not using a proper return, or am possibly making my array poorly. The array should also be scanning text document to check how many times a word appears and was wondering if the way my loop is written it will actually return the proper value.
Mã:
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;
   }
}
 

Admin

Administrator
Thành viên BQT
#2
You are not calling the puntuation() method from main method, and also wordCheck() method from puntuation() method
Mã:
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;
   }
}
 
Top