这是一个家庭作业。我正在尝试制作一个程序,在文本文件中搜索特定单词,然后打印出单词的频率。
public class WordFreq extends Echo{
ArrayList<WordCount>array1=new ArrayList<WordCount>();
String[] words;
int wordsTotal=0;
public WordFreq(String f, String x) throws IOException
{
super(f);
words=x.toLowerCase().split(" ");
}
public void processLine(String line){
String[] lines=line.toLowerCase().split(" ");
wordsTotal=wordsTotal+lines.length;
for(int j=0; j<lines.length; j++){
WordCount alpha=new WordCount(lines[j]);
alpha.incCount();
array1.add(alpha);}
for(int x=0; x<array1.size(); x++){
for(int y=0; y<array1.size(); y++){
if(array1.get(x).equals(array1.get(y))&&(x!=y)){
for(int i = 0; i< array1.get(y).getCount(); i++){
array1.get(x).incCount();
}
array1.remove(y);
}
}
}
}
public void reportFrequencies(){
for(int i = 0; i<array1.size();i++){
// System.out.println(array1.get(i).getWord()+" "+array1.get(i).getCount());
}
int currentWord=0;
for(int x=0; x<words.length; x++){
for(int y=0; y<array1.size(); y++){
if(words[x].equals(array1.get(y).getWord())){
currentWord=array1.get(y).getCount();}}
System.out.print(words[x]+" ");
System.out.printf("%.4f",(double)currentWord/wordsTotal);
}
}
}
这是我的主要方法:
public class FreqStudy{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(System.in);
System.out.println("enter file name");
String fileName = scan.next();
Scanner scan2 = new Scanner(System.in);
System.out.println("enter words to search for");
System.out.println("enter lower case, separated by spaces");
String wordString = scan2.nextLine();
WordFreq f = new WordFreq(fileName,wordString);
f.readLines();
f.reportFrequencies();
}
}
我正在使用简·奥斯汀 (Jane Austen) 所著的《艾玛》一书的 .txt 文件。这是我在运行程序并尝试搜索单词时收到的错误消息:
java.lang.IndexOutOfBoundsException: Index: 906, Size: 906
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at WordFreq.processLine(WordFreq.java:26)
at Echo.readLines(Echo.java:16)
at FreqStudy.main(FreqStudy.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)"
以下是 Echo 和 WordCount 的代码:
public class WordCount{
private String word;
private int count;
public WordCount(String w){
word = w;
count = 0;
}
public String getWord(){
return word;}
public int getCount(){
return count;}
public void incCount(){count++;}
public String toString() {
return(word + " --- " + count);
}
public boolean equals(Object other){
WordCount i = (WordCount)other;
return (this.word.equals(i.word));
}
}
回声:
import java.util.Scanner;
import java.io.*;
public class Echo{
String fileName; // external file name
Scanner scan; // Scanner object for reading from external file
public Echo(String f) throws IOException
{
fileName = f;
scan = new Scanner(new FileReader(fileName));
}
public void readLines(){ // reads lines, hands each to processLine
while(scan.hasNext()){
processLine(scan.nextLine());
}
scan.close();
}
public void processLine(String line){ // does the real processing work
System.out.println(line);
}
}
我的代码的第 26 行是:
for(int i = 0; i< array1.get(y).getCount(); i++)