2012-03-30 8 views
-2

コード全体を実行します。私は単純な.txtファイルを入力して単語を検索することができます。 -48 SearchEngine.main(SearchEngine.java:150)でJava Search Engineのデバッグ

ライン150は、(int型J =のためである:それは単語を要求した後、それは "メイン" java.lang.ArrayIndexOutOfBoundsExceptionスレッドで

Exceptionを返します。 0; jの

任意のヘルプのデバッグ

この任意の単語のための.txtファイルを検索することができる必要があり、基本的な検索エンジンのプログラムです

割り当てリンク:http://cis-linux1.temple.edu/~yates/cis1068/sp12/homeworks/concordance/concordance.html

?。 searchWordが配列されていないとき
import java.util.*; 
import java.io.*; 


public class SearchEngine { 


    //Counts the number of words in the file 
    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 String[] getNumOfDistinctWords (String[] x) throws FileNotFoundException { 

     HashSet<String> distinctWords = new HashSet<String>(); 
     for(int i=0; i<x.length; i++){ 
      distinctWords.add(x[i]); 
     } 

     String[] distinctWordsArray = new String[distinctWords.size()]; 
     int i = 0; 
     for(String word : distinctWords){ 
      distinctWordsArray[i] = word; 
      i++; 
     } 


     return distinctWordsArray; 
    } 

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

      //Output the number of words in the file 
      System.out.println("Number of words is: " + NUM_WORDS); 


      String[] allWordsArray = new String[NUM_WORDS]; 
      readInWords(input, allWordsArray); 
      Arrays.sort(allWordsArray); 
      String[] distinctWordsArray = getNumOfDistinctWords(allWordsArray); 

      //Output the number of distinct words 
      System.out.println("Number of distinct words is: " + distinctWordsArray.length); 
      System.out.println("Finished creating distinctWordsArray"); 

      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[distinctWordsArray.length][10]; 
      int [] wordCountArray = new int[distinctWordsArray.length]; 


      int lineNum = 0; 
      while (lineNum < concordanceArray.length) { 
       Scanner scan = new Scanner(concordanceArray[lineNum]); 

       while (scan.hasNext()) { 
        //Find the position the word appears on the line, if word not found returns a number less than 0 
        int wordPos = Arrays.binarySearch(distinctWordsArray, scan.next()); 

        if(wordPos > -1){ 
         wordCountArray[wordPos] += 1; 
        } 


        for(int i = 0; i < invertedIndex.length; i++) { 
         for(int j = 0; j < 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<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 
+0

お試しください。 ArrayIndexOutOfBoundsExceptionはどういう意味ですか? – j4y

+0

大学でユニットテストをいつ行うのですか? – Jayan

+1

Debuggin?本当に? - 元のコードの行番号150には何がありますか? –

答えて

1

int wordPos = Arrays.binarySearch(allWordsArray, searchWord);

wordPosは負となります。したがって、for (int j = 0; j<invertedIndex[wordPos].length; j++) {

invertedIndex[wordPos]はあなたのケースでは、配列の負のインデックスにアクセスしようとするだろう、-48

あなたはループの前にこのような何かを行う必要があります。

if(wordPos < 0){ 
    // Do something 
}else { 
    for (int j = 0; j<invertedIndex[wordPos].length; j++) { 
    ... 
} 

あなたべきJavadoc、特にreturnsのドキュメントを読んでください。 -48についてのあなたの返事が表示されます。

関連する問題