Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

Cannnot find "Array Out of Bounds Exception" Java

I am creating a search engine that reads in a text file, and prints out a word that a user can search for. I'm currently creating an index of arrays to be searched for. More information can be found here: http://cis-linux1.temple.edu/~yates/cis1068/sp12/homeworks/concordance/concordance.html

When I run this program right now, I get an "Array Index Out of Bounds Exception"

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 43 at SearchEngine.main(SearchEngine.java:128)

Can anyone help debug?

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

Main

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

} //class

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
for(int j = 0; j < invertedIndex[i].length; i++) {

should probably be

j++

not

i++

Update after your fix.

That means that Arrays.binarySearch(vocabArray, scan.next()) is not finding the item being searched for. You cannot assume that the vocabArray has the item you are searching for. You will need to add an if(... < 0) for the binarySearch call.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...