尝试在文件中查找出现次数最多的单词时,Java ArrayIndexOutOfBoundsException不断出现

时间:2018-08-26 02:38:03

标签: 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 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进行此赋值(没有其他内置函数,但强烈建议也使用正则表达式其余的作业)。我尝试在其中放置私有的FileReaderBufferedReader,以查看它们是否会更好。感谢您的建议!

2 个答案:

答案 0 :(得分:1)

在此循环中:

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

您检查以确保imostOccur的范围内,但不在count的范围内。我将添加一个条件来检查以确保它处于边界。如:

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

答案 1 :(得分:1)

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

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

}

}