[英]Cannot return array to main method
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. 我试图从wordCheck方法返回intArray并且错误找不到符号-当我尝试从数组中打印值时,变量intArray出现在第10,11和12行中。 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.
该数组还应该在扫描文本文档,以检查一个单词出现了多少次,并且想知道我的循环的编写方式是否实际上将返回正确的值。
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;
}
}
You are not calling the puntuation()
method from main method, and also wordCheck()
method from puntuation()
method 你是不是调用
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.