Java 为什么我会有一个ArrayIndex的BoundsException?

Java 为什么我会有一个ArrayIndex的BoundsException?,java,eclipse,Java,Eclipse,创建数组并使用split方法,还使用数组创建my 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;

创建数组并使用split方法,还使用数组创建my 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');

}

}ArrayIndexOutOfBoundsException表示您正在访问无效数组中的偏移量。你在那条线上看到的任何偏移量都是不允许的。现在,为什么
索引
不是有效的偏移量

上次向数组中添加内容后,您增加了索引,因此它不再是有效的偏移量。

请尝试以下操作:

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

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

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

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

}

}

请注意设置为
索引
变量的值,例如,在每次迭代之前添加
System.out.println(index)
。我认为您应该使用
二进制搜索
方法的返回值,因为
索引
是有意义的。谢谢你的建议我意识到如果我在第一个while循环中不增加索引,我会得到一个NullPointerException。成功了!谢谢。