2015-05-04 140 views
-2

我意识到这个问题之前已经被问到过,但我无法理解我读到的很多答案。我一直在这个代码了一会儿:Java数组索引溢出异常(-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”匾额。本质上,Pslams从1 - 99的顺序(但不是每个诗篇都包含在内,例如,文本文件中不存在pslam 4)

我试图使用indexOf()来查找哪个索引首先出现一个角色。然后,我将能够将其向前移动到数组中的一个索引并找到标题,因为数组中的信息如下所列:

[2,psalm 2 title,3,psalm 3 title,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

你得到的,因为你的代码的例外是无效

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]所以你得到ArrayIndexOutOfBoundsException