Java数组索引越界异常(-1)?

Java Array Index out of Bounds Exception (-1)?

我知道以前有人问过这个问题,但我无法理解我阅读的大部分答案。我已经研究这段代码一段时间了:

  static ArrayList<String> psalmsTitlesArray = new ArrayList<>(47);
    static ArrayList<String> psalmsNumbersArray = new ArrayList<>(47);
    static String psalmTextFileArray[] = new String[47];
    static int index = 0;

    public static void main(String[] args) throws IOException {
        //this program demonstrates a binary array search. It is going to search
        //the text file "Psalms.txt" for a number list of the pslams.
        // eg. the user wants to see psalm 7
        // the program will display "prayer of the virtuous under persecution".
        BufferedReader fileRead = new BufferedReader(new FileReader("Psalms.txt")); //create a BufferedReader to read the file
        String fileLine = "";
        int lineNumber = 1;
        for (int i = 0; i < 47; i++) {
            fileLine = fileRead.readLine(); // stores each line of text in a String 
            if (fileLine == null) { //if the line is blank
                break; // don't read it
            }
            psalmTextFileArray[i] += fileLine;
            if (lineNumber % 2 == 0) { //if the line is not an even number 
                psalmsTitlesArray.add(fileLine); //add the line to the Titles array
                lineNumber++;
            } else { //otherwise,
                psalmsNumbersArray.add(fileLine); //add it to the numbers array
                lineNumber++;
            }
        }
        String userInput = JOptionPane.showInputDialog(null, "What psalm would you like me to search for?", "Psalm Finder",
                JOptionPane.QUESTION_MESSAGE);
        if (userInput == null) {
            System.exit(0);
        }
        int numberInput = Integer.parseInt(userInput);

        binarySearch(psalmsNumbersArray, 0, (psalmsNumbersArray.size() - 1), userInput);
        for (int i = 0; i < psalmTextFileArray.length; i++) {
        index = psalmTextFileArray[i].indexOf(numberInput);
        }
        JOptionPane.showMessageDialog(null, "I found Psalm #" + userInput + ". It is: \n" + psalmTextFileArray[index]);

    }

    public static boolean binarySearch(ArrayList<String> myPsalmsArray, int left, int right, String searchForPsalm) {
        int middle;
        if (left > right) {
            return false;
        }
        middle = (left + right) / 2;
        if (myPsalmsArray.get(middle).equals(searchForPsalm)) {
            return true;
        }
        if (searchForPsalm.compareTo(myPsalmsArray.get(middle)) < 0) {
            return binarySearch(myPsalmsArray, left, middle - 1,
                    searchForPsalm);
        } else {
            return binarySearch(myPsalmsArray, middle + 1, right,
                    searchForPsalm);
        }
    }
}

此代码从文件 "Psalm.txt" 中读取。本质上,Pslam 的顺序是从 1 - 99(但不是每个 Psalm 都包括在内,例如,文本文件中不存在 pslam 4)

我正在尝试使用 indexOf() 查找字符首次出现在哪个索引处。然后,我可以将它在数组中向前移动一个索引并找到标题,因为数组中的信息如下所示:

[2, 诗篇 2 标题, 3, 诗篇 3 标题, 4...] (等等)

这是导致问题的一小段代码:

for (int i = 0; i < psalmTextFileArray.length; i++) {
            index = psalmTextFileArray[i].indexOf(numberInput);
            }
            JOptionPane.showMessageDialog(null, "I found Psalm #" + userInput + ". It is: \n" + psalmTextFileArray[index]);

所以,如果我能在数组中找到 2 的索引,将它向前移动一个索引,我就会得到标题。 此外,索引始终抛出 -1 异常。这是什么意思?

抱歉,如果这令人困惑,我可以澄清任何不清楚的地方(我会尽力而为!)

您遇到异常是因为您的代码无效

 int numberInput = Integer.parseInt(userInput);

    binarySearch(psalmsNumbersArray, 0, (psalmsNumbersArray.size() - 1), userInput);
    for (int i = 0; i < psalmTextFileArray.length; i++) {
    index = psalmTextFileArray[i].indexOf(numberInput);
    }
    JOptionPane.showMessageDialog(null, "I found Psalm #" + userInput + ". It is: \n" + psalmTextFileArray[index]);

在上面的代码中 psalmTextFileArray[i] 将 return 一个 String 对象。那么你正在做 indexOf(numberInput)

JavaDoc

所述

the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.

因此,如果您的 numberInput 不在 String 对象中,您的 index 将变为 -1,因为

index = -1

并且在下一行中,当您调用 showMessageDialog 时,您正在使用 -1 访问数组的位置,因为您正在使用 psalmTextFileArray[index] 所以您得到 ArrayIndexOutOfBoundsException