2012-03-29 42 views
-2

我正在創建一個讀取文本文件並打印出用戶可以搜索的詞的搜索引擎。我目前正在創建一個要搜索的數組的索引。更多信息可以在這裏找到:http://cis-linux1.temple.edu/~yates/cis1068/sp12/homeworks/concordance/concordance.htmlCannnot找不到「數組溢出異常」Java

當我現在運行這個程序,我得到一個「數組索引越界異常的」

異常線程「main」 java.lang.ArrayIndexOutOfBoundsException:43 在SearchEngine.main(SearchEngine.java:128)

任何人都可以幫助調試嗎?

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() && i<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() && i<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<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() && i<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<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 < invertedIndex.length; i++) { 
      for(int j = 0; j < invertedIndex[i].length; i++) { 
      if (invertedIndex[i][j] == 0) { 
      invertedIndex[i][j] = lineNum; 
      break; 
      } } } 
      } 
     lineNum++; 
     } 
System.out.println("Finished creating invertedIndex"); 

} 

    catch (FileNotFoundException exception) { 
    System.out.println("File Not Found"); 
} 




} //main 

} //類

+1

你在哪裏得到異常? – hvgotcodes 2012-03-29 22:37:45

+0

定義「找不到」;該例外將精確地顯示錯誤發生的位置。 – 2012-03-29 22:39:52

+0

異常在線程「主」 java.lang.ArrayIndexOutOfBoundsException:43 \t在SearchEngine.main(SearchEngine.java:128) – user1302023 2012-03-29 22:41:21

回答

6
for(int j = 0; j < invertedIndex[i].length; i++) { 

也許應該

j++ 

i++ 

修復後更新。

這意味着Arrays.binarySearch(vocabArray, scan.next())未找到正在搜索的項目。你不能假設vocabArray有你正在搜索的項目。您需要爲binarySearch調用添加if(... < 0)

+0

修復它到j ++,現在我得到這個: – user1302023 2012-03-29 22:54:34

+0

線程「main」中的異常java.lang.ArrayIndexOutOfBoundsException:-1 \t at SearchEngine.main(SearchEngine.java:126) – user1302023 2012-03-29 22:54:43

+0

但是,如果您按照代碼,它基本上從txt文件中獲取每個單詞,並將其添加到vocabArray。然後它從文件中取出每一行,並將其添加到concordanceArray。二進制搜索,搜索concordanceArray的每一行中的每個單詞並返回它在其中找到的vocabArray中的索引。所以理論上,vocabArray應該有它。 – user1302023 2012-03-29 23:15:01