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

Java ArrayIndexOutOfBoundsException在试图查找文件中最常出现的单词时不断出现,java,arrays,eclipse,arraylist,indexoutofboundsexception,Java,Arrays,Eclipse,Arraylist,Indexoutofboundsexception,我目前正在构建一个程序,它读取一个文件并打印最常出现的单词,以及每个单词出现的次数: package WordLookUp; import java.util.*; import java.io.*; import java.lang.*; public class WordLookUp { private String[] mostWords; private Scanner reader; private String line; private Fil

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

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();



    }

}
packagewordlookup;
导入java.util.*;
导入java.io.*;
导入java.lang.*;
公共类查字{
私有字符串[]mostWords;
私人扫描仪阅读器;
专用字符串线;
私人文件阅读器;
专用缓冲读取程序br;
私人名单原件;
私有字符串令牌=”;
公共WordLookUp(字符串文件)引发异常{
this.reader=新扫描仪(新文件(File));
this.original=新的ArrayList();
而(this.reader.hasNext()){//读取文件并将其存储在字符串中
this.token=this.reader.next();
this.original.add(token);//将其添加到我的arrayList中
}
}
public void findMostOccurringWords()公共无效{
List mostOccur=new ArrayList();
列表计数=新的ArrayList();
int计数器=0;
this.mostWords=this.token.split(“”;//在mostWords数组列表中存储读取行
试一试{
对于(int i=0;i
所以当我继续运行我的文件时,它抛出我给它的异常:“非法索引”。我想这是我的
查找单词的方法。对我来说,逻辑是正确的,但我不知道为什么它会抛出一个
ArrayIndexOutOfBoundsException
。我尝试使用for循环,并尝试从
inti=0转到I
,但这也不起作用。我的逻辑错了吗?我不允许使用
hashmap
,我们的教授给了我们一个提示,我们可以轻松地使用数组和
ArrayLists
(没有其他内置函数,但强烈建议在剩余的作业中也使用正则表达式)。我在上面放了一个私有的
FileReader
BufferedReader
,我想看看它们是否能更好地工作。谢谢你的建议

在此循环中:

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

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

你能试着使用以下代码吗?我认为你目前的算法是错误的

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();

}
公共类WordLookUp{
私人名单原件;
private List mostOccur=new ArrayList();
私有列表计数=新的ArrayList();
公共WordLookUp(字符串文件)引发异常{
试试(扫描仪阅读器=新扫描仪(新文件));){
this.original=新的ArrayList();
字符串标记=”;
而(reader.hasNext()){//读取文件并将其存储在字符串中
token=reader.next();
this.original.add(token);//将其添加到我的arrayList中
FindMostcourringwords(标记);
}
}
}
public void findMostOccurringWords(字符串标记){
int计数器=0;
字符串[]mostWords=token.split(“”;//在mostWords数组列表中存储读取行
试一试{
for(int i=0;i

}

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

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();

}
公共类WordLookUp{
私人名单原件;
private List mostOccur=new ArrayList();
私有列表计数=新的ArrayList();
公共WordLookUp(字符串文件)引发异常{
试试(扫描仪阅读器=新扫描仪(新文件));){
this.original=新的ArrayList();
字符串标记=”;
而(reader.hasNext()){//读取文件并将其存储在字符串中
token=reader.next();
this.original.add(token);//将其添加到我的arrayList中
FindMostcourringwords(标记);
}
}
}
public void findMostOccurringWords(字符串标记){
int计数器=0;
字符串[]mostWords=token.split(“”;//在mostWords数组列表中存储读取行
试一试{
for(int i=0;i