我正在研究一个问题,但由于刚开始学习Java而感到非常困惑。我可以理解的任何帮助都会很棒。我必须编写一个具有两个类的程序。主类将从文件中读取内容,并使用第二类来查找文件中相同单词被重复的次数,然后将它们添加到包含单词和单词重复次数的数组中。我可以阅读文件部分。我只是似乎不知道如何从第二个类调用方法以将单词添加到数组并增加计数器。到目前为止,这是我的代码,如果您运行它,您将看到多少错误会告诉我我有多新。
import java.io.*;
public class Words{
public static void main (String [] args)
{
ProcessInput();
System.out.println("\nprogram finished");
}
public static WordList ProcessInput( )
{
BufferedReader inputFile;
String inputLine;
String[] word;
WordList words;
try
{
inputFile=new BufferedReader(new FileReader ("inputFile.txt"));
inputLine = inputFile.readLine();
while (inputLine !=null)
{
word=inputLine.toLowerCase().split(" ");
for (int i=0; i
System.out.println (word[i]);
words=addWord(word[i]);
}
inputLine = inputFile.readLine();
}
inputFile.close();
}
catch (IOException ioe)
{
System.out.println (ioe.getMessage());
ioe.printStackTrace ();
}
return words;
}
}
class WordList {
String [] words;
int wordcount;
public WordList ( ){
words= new String [1000];
wordcount=0;
}
public String addWord (String word) {
words[wordcount]=word;
wordcount=+1;
return words[wordcount];
}
public void printList (){
for (int i=0; i
System.out.println (words[i]);
}
}
}