Java数组索引超出边界异常(-1)?

时间:2015-05-04 13:13:05

标签: java arrays indexoutofboundsexception

我意识到之前已经问过这个问题,但是我无法理解我读到的很多答案。我一直在研究这段代码:

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

此代码从文件&#34; Psalm.txt&#34;中读取。从本质上讲,Pslams按顺序从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异常。这是什么意思?

道歉,如果这令人困惑,我可以澄清任何不清楚的事情(我会尽我所能!)

1 个答案:

答案 0 :(得分: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]中的

将返回一个String对象。然后你正在做indexOf(numberInput)

JavaDoc

中所述
  

此对象表示的字符序列中第一次出现的字符的索引,如果未出现该字符,则为-1。

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

index = -1

在下一行,当您致电showMessageDialog时,您正在使用-1访问数组的位置,因为您正在使用psalmTextFileArray[index]