title: Java Array Index Out of Bounds Exception
body: <p>I run through the entire code. I am able to enter a simple .txt file to search for a word. After it asks for a word, it returns </p>
  
  <p>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -48
      at SearchEngine.main(SearchEngine.java:150)</p>
  
  <p>Line 150 is for (int j = 0; j
  
  <p>Any help debugging? </p>
  
  <p>This is basic search engine program that should be able to search a .txt file for any word.</p>
  
  <p>Assignment link: <a href="http://cis-linux1.temple.edu/~yates/cis1068/sp12/homeworks/concordance/concordance.html" rel="nofollow">http://cis-linux1.temple.edu/~yates/cis1068/sp12/homeworks/concordance/concordance.html</a></p>
  
  <hr>
  
  <pre><code>import java.util.*;
  import java.io.*;
  
  
  public class SearchEngine {
  
  
      public static int getNumberOfWords (File f) throws FileNotFoundException {
          int numWords = 0;
          Scanner scan = new Scanner(f);
          while (scan.hasNext()) {
          numWords++;
          scan.next();
          }
          scan.close();
  
          return numWords;
      }
  
      public static void readInWords (File input, String [] x) throws FileNotFoundException {
          Scanner scan = new Scanner(input);
          int i = 0;
          while (scan.hasNext() &amp;&amp; i&lt;x.length) {
              x[i] = scan.next();
              i++;
              }
          scan.close();
      }
  
      public static int getNumOfDistinctWords (File input, String [] x) throws FileNotFoundException {
          Scanner scan = new Scanner(input);
          int count = 0;
          int i = 1;
          while (scan.hasNext() &amp;&amp; i&lt;x.length) {
          if (!x[i].equals(x[i-1])) {
          count++;
          }
          i++;
          }
          scan.close();
          return count;
      }
  
      public static void readInDistinctWords (String [] x, String [] y) {
          int i = 1;
          int k = 0;
          while (i&lt;x.length) {
              if (!x[i].equals(x[i-1])) {
              y[k] = x[i];
              k++;
              }
          i++;
          }
      }
  
      public static int getNumberOfLines (File input) throws FileNotFoundException {
          int numLines = 0;
          Scanner scan = new Scanner(input);
          while (scan.hasNextLine()) {
              numLines++;
              scan.nextLine();
              }
          scan.close();
          return numLines;
      }
  
      public static void readInLines (File input, String [] x) throws FileNotFoundException {
          Scanner scan = new Scanner(input);
          int i = 0;
          while (scan.hasNextLine() &amp;&amp; i&lt;x.length) {
              x[i] = scan.nextLine();
              i++;
              }
          scan.close();
      }
  
  
  
  public static void main(String [] args) {
  
       try {
  
          //gets file name
      System.out.println("Enter the name of the text file you wish to search");
          Scanner kb = new Scanner(System.in);
          String fileName = kb.nextLine();
          String TXT = ".txt";
          if (!fileName.endsWith(TXT)) {
              fileName = fileName.concat(TXT);
          }
  
          File input = new File(fileName);
  
      //First part of creating index
      System.out.println("Creating vocabArray");
      int NUM_WORDS = getNumberOfWords(input);
      //System.out.println(NUM_WORDS);
      String [] wordArray = new String[NUM_WORDS];
      readInWords(input, wordArray);
      Arrays.sort(wordArray);
      int NUM_DISTINCT_WORDS = getNumOfDistinctWords(input, wordArray);
      String [] vocabArray = new String[NUM_DISTINCT_WORDS];
      readInDistinctWords(wordArray, vocabArray);
      System.out.println("Finished creating vocabArray");
  
  
  
      System.out.println("Creating concordanceArray");
      int NUM_LINES = getNumberOfLines(input);
      String [] concordanceArray = new String[NUM_LINES];
      readInLines(input, concordanceArray);
      System.out.println("Finished creating concordanceArray");
  
  
  
      System.out.println("Creating invertedIndex");
      int [][] invertedIndex = new int[NUM_DISTINCT_WORDS][10];
      int [] wordCountArray = new int[NUM_DISTINCT_WORDS];
      int lineNum = 0;
          while (lineNum&lt;concordanceArray.length) {
              Scanner scan = new Scanner(concordanceArray[lineNum]);
              while (scan.hasNext()) {
                  int wordPos = Arrays.binarySearch(vocabArray, scan.next());
                  wordCountArray[wordPos]+=1;
                  for(int i = 0; i &lt; invertedIndex.length; i++) {
                  for(int j = 0; j &lt; invertedIndex[i].length; j++) {
                  if (invertedIndex[i][j] == 0) {
                  invertedIndex[i][j] = lineNum;
                  break;
                  } } }
                  }
              lineNum++;
              }
      System.out.println("Finished creating invertedIndex");
  
      }
  System.out.println("Enter a word to be searched (type quit to exit program)");
      Scanner keyboard = new Scanner(System.in);
      String searchWord = keyboard.next();
      while (!searchWord.equals("quit")) {
          int counter = 0;
  
                      int wordPos = Arrays.binarySearch(allWordsArray, searchWord);
              for (int j = 0; j&lt;invertedIndex[wordPos].length; j++) {
                  if(invertedIndex[wordPos][j] != 0) {
                         int number = invertedIndex[wordPos][j];
                         String printOut = concordanceArray[number];
                                                 System.out.print(number);
                                                 System.out.print(" :");
                                                 System.out.println(printOut);
                                      }
              }
  
              }        
  
  
  
          catch (FileNotFoundException exception) {
          System.out.println("File Not Found");
      }
  
  
  
  
      } //main
  } //class
  </code></pre>