gpt4 book ai didi

尝试查找文件中最常出现的单词时,Java ArrayIndexOutOfBoundsException 不断出现

转载 作者:行者123 更新时间:2023-12-02 01:58:14 27 4
gpt4 key购买 nike

我目前正在构建一个程序,该程序读取文件并打印最常出现的单词以及每个单词出现的次数,如下所示:

package WordLookUp;

import java.util.*;
import java.io.*;
import java.lang.*;

public class WordLookUp {

private String[] mostWords;
private Scanner reader;
private String line;
private FileReader fr;
private BufferedReader br;
private List<String> original;
private String token = " ";


public WordLookUp(String file) throws Exception {
this.reader = new Scanner(new File(file));
this.original = new ArrayList<String>();



while (this.reader.hasNext()) { //reads file and stores it in string
this.token = this.reader.next();
this.original.add(token); //adds it to my arrayList
}


}

public void findMostOccurringWords() {
List<String> mostOccur = new ArrayList<String>();
List<Integer> count = new ArrayList<Integer>();
int counter = 0;


this.mostWords = this.token.split(" "); //storing read lines in mostWords arrayList

try {

for (int i = 0; i < original.size(); i++) {
if (this.original.equals(this.mostWords[i])) {
counter++; //increase counter
mostOccur.add(this.mostWords[i]);
count.add(counter);
}
}

for (int i = 0; i < mostOccur.size(); i++) {
System.out.println("Word: " + mostOccur.get(i) + " count: " + count.get(i));
}

} catch (ArrayIndexOutOfBoundsException ae) {
System.out.println("Illegal index");
}
}






}


package WordLookUp;

import java.util.*;
import java.io.*;


public class Main {

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub


WordLookUp wL = new WordLookUp("tiny1.txt");

wL.findMostOccurringWords();



}

}

因此,当我继续运行我的文件时,它会抛出我给它的异常:“非法索引”。我想这是我的findMostOccuringWords方法。对我来说,逻辑感觉是正确的,但我不知道为什么它会抛出 ArrayIndexOutOfBoundsException 。我尝试使用 for 循环并尝试从 int i = 0 to i < mostOccur.size() - 1 开始但这也不起作用。我的逻辑有错吗?我不允许使用 hashmap我们的教授给了我们一个提示,我们可以使用数组和 ArrayLists 轻松完成这项作业。 (没有其他内置函数,但强烈建议在其余作业中使用正则表达式)。我放了私有(private)FileReaderBufferedReader在那里,因为我想看看它们是否会更好地工作。谢谢您的建议!

最佳答案

您可以尝试使用以下代码吗?我认为你当前的算法是错误的。

public class WordLookUp {
private List<String> original;
private List<String> mostOccur = new ArrayList<String>();
private List<Integer> count = new ArrayList<Integer>();


public WordLookUp(String file) throws Exception {
try(Scanner reader = new Scanner(new File(file));){
this.original = new ArrayList<String>();
String token = " ";
while (reader.hasNext()) { //reads file and stores it in string
token = reader.next();
this.original.add(token); //adds it to my arrayList
findMostOccurringWords(token);
}
}
}

public void findMostOccurringWords(String token) {
int counter = 0;
String[] mostWords = token.split(" "); //storing read lines in mostWords arrayList
try {
for (int i = 0; i < mostWords.length; i++) {
for(int j = 0; j < this.original.size(); j++) {
if (original.get(j).equals(mostWords[i])) {
counter++; //increase counter
}
}
if (mostOccur.contains(mostWords[i])) {
count.set(mostOccur.indexOf(mostWords[i]),counter);
}else {
mostOccur.add(mostWords[i]);
count.add(counter);
}
}
} catch (ArrayIndexOutOfBoundsException ae) {
System.out.println("Illegal index");
}
}

public void count() {
for (int i = 0; i < mostOccur.size(); i++) {
System.out.println("Word: " + mostOccur.get(i) + " count: " + count.get(i));
}
}
}

public class Main {

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
WordLookUp wL = new WordLookUp("F:\\gc.log");

wL.count();

}

}

关于尝试查找文件中最常出现的单词时,Java ArrayIndexOutOfBoundsException 不断出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52022767/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com