为什么我得到一个ArrayIndexOutOfBoundsException?

时间:2017-04-06 22:35:49

标签: java eclipse

创建了数组并使用了split方法,还创建了一个带有数组的NameInfo类的实例:

public class NameSearch {

public static void main(String[] args) throws IOException {

    NameInfo[] nameList = new NameInfo[151671];

    Scanner keyboard = new Scanner(System.in);
    String inString = null;
    String selectedName;
    String anotherName;
    int numberOfNames = 0;
    int index = 0;

    FileReader freader = new FileReader(new File("LastNames2000Census.txt"));
    Scanner inputFile = new Scanner(freader);

    while (inputFile.hasNext()) {
        inString = inputFile.nextLine();
        String[] nextString = inString.split(",");
        String nextTitle = nextString[0];
        int nextRank = Integer.parseInt(nextString[1]);
        int nextNumOfOccurrences = Integer.parseInt(nextString[2]);
        double nextProp100k = Double.parseDouble(nextString[3]);
        double nextCumProp100k = Double.parseDouble(nextString[4]);
        nameList[index] = new NameInfo(nextTitle, nextRank, nextNumOfOccurrences, nextProp100k, nextCumProp100k);
        index++;
    }
使用插入和二进制泛型方法对

数组进行排序和搜索。错误发生在print语句的第一行:

do {
        ArraySort.insertionSort(nameList);
        boolean found = false;

        System.out.println("Hello, enter a last name.");
        selectedName = keyboard.nextLine();
        ArraySearch searcher = new ArraySearch(nameList);
        if (searcher.binarySearch(nameList, 0, 0, selectedName) == -1)
            System.out.println(selectedName + " was not found. Try again.");
        else {
            System.out.println(
                    selectedName + " was found at index " + index + "\n ranked: " + nameList[index].getRank()
                            + "/n number of occurences: " + nameList[index].getNumOfOccurrences()
                            + "\n proportion per 100,000 people: " + nameList[index].getProp100k()
                            + "\n cumulative proportion per 100,000 people: " + nameList[index].getCumProp100k());
        }
        System.out.println("Want to search another name? (Y/N)");
        anotherName = keyboard.nextLine();
    } while (anotherName.charAt(0) == 'y' || anotherName.charAt(0) == 'Y');

}

}

4 个答案:

答案 0 :(得分:0)

ArrayIndexOutOfBoundsException表示您正在访问无效的数组中的偏移量。无论你在那条线上看到什么偏移都是出界的。现在,为什么index不是有效的偏移?

您在最后一次向数组添加内容后后递增index ,因此它不再是有效的偏移量。

答案 1 :(得分:0)

在尝试访问元素nextString [i]之前,确保字符串的数组nextString的长度大于索引i。 ... if(i> nextString.length)   ... = nextString [i];

答案 2 :(得分:0)

我已经修复了越界异常(谢谢),但程序似乎没有遍历文件。当它运行时,我只从文件的第一行获取数据,无论我输入什么名称:

while (inputFile.hasNext()) {
        inString = inputFile.nextLine();
        String[] nextString = inString.split(",");
        String nextTitle = nextString[0];
        int nextRank = Integer.parseInt(nextString[1]);
        int nextNumOfOccurrences = Integer.parseInt(nextString[2]);
        double nextProp100k = Double.parseDouble(nextString[3]);
        double nextCumProp100k = Double.parseDouble(nextString[4]); 
        nameList[index] = new NameInfo(nextTitle, nextRank, nextNumOfOccurrences, nextProp100k, nextCumProp100k);
        index++;
    }
    do {
        ArraySort.insertionSort(nameList);
        boolean found = false;

        System.out.println("Hello, enter a last name.");
        selectedName = keyboard.nextLine();
        ArraySearch searcher = new ArraySearch(nameList);
        index = searcher.binarySearch(nameList, 0, 0, selectedName);
        if (index == -1)
            System.out.println(selectedName + " was not found. Try again.");
        else {
            System.out.println(
                    selectedName + " was found at index " + index + "\n ranked: " + nameList[index].getRank()
                            + "\n number of occurences: " + nameList[index].getNumOfOccurrences()
                            + "\n proportion per 100,000 people: " + nameList[index].getProp100k()
                            + "\n cumulative proportion per 100,000 people: " + nameList[index].getCumProp100k());
        }
        System.out.println("Want to search another name? (Y/N)");
        anotherName = keyboard.nextLine();
    } while (anotherName.charAt(0) == 'y' || anotherName.charAt(0) == 'Y');

}

}

答案 3 :(得分:-1)

试试这个:

index = searcher.binarySearch(nameList, 0, 0, selectedName);
if (index == -1)
    System.out.println(selectedName + " was not found. Try again.");
else {...}