Cannnot查找和QUOT;数组出界异常&QUOT的; Java的 [英] Cannnot find "Array Out of Bounds Exception" Java

查看:203
本文介绍了Cannnot查找和QUOT;数组出界异常&QUOT的; Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建读取文本文件,并打印出一个字,用户可以搜索的搜索引擎。我目前正在创建要搜索一个数组的索引。更多信息可以在这里找到:的http://cis-linux1.temple.edu/~yates/cis1068/sp12/homeworks/concordance/concordance.html

当我现在运行这个程序,我得到一个数组索引越界异常的

异常线程mainjava.lang.ArrayIndexOutOfBoundsException:43
    在SearchEngine.main(SearchEngine.java:128)

谁能帮助调试?

 进口的java.util。*;
进口java.io. *;
公共类的搜索引擎{
公共静态INT getNumberOfWords(文件F)抛出FileNotFoundException异常{
    INT NUMWORDS = 0;
    扫描程序扫描=新的扫描仪(F);
    而(scan.hasNext()){
    NUMWORDS ++;
    scan.next();
    }
    scan.close();    返回NUMWORDS;
}公共静态无效readInWords(文件输入的String []×)抛出FileNotFoundException异常{
    扫描程序扫描=新的扫描仪(输入);
    INT I = 0;
    而(scan.hasNext()及&放大器; I&下; x.length){
        X [I] = scan.next();
        我++;
        }
    scan.close();
}公共静态INT getNumOfDistinctWords(文件输入的String []×)抛出FileNotFoundException异常{
    扫描程序扫描=新的扫描仪(输入);
    诠释计数= 0;
    INT I = 1;
    而(scan.hasNext()及&放大器; I&下; x.length){
    如果(!×〔Ⅰ〕.equals(X [I-1])){
    算上++;
    }
    我++;
    }
    scan.close();
    返回计数;
}公共静态无效readInDistinctWords(字符串[]×,的String [] Y){
    INT I = 1;
    INT K = 0;
    而(I< x.length){
        如果(!×〔Ⅰ〕.equals(X [I-1])){
        Y [k]的= X [I];
        ķ++;
        }
    我++;
    }
}公共静态INT getNumberOfLines(文件输入)抛出FileNotFoundException异常{
    INT numLines = 0;
    扫描程序扫描=新的扫描仪(输入);
    而(scan.hasNextLine()){
        numLines ++;
        scan.nextLine();
        }
    scan.close();
    返回numLines;
}公共静态无效readInLines(文件输入的String []×)抛出FileNotFoundException异常{
    扫描程序扫描=新的扫描仪(输入);
    INT I = 0;
    而(scan.hasNextLine()及&放大器; I&下; x.length){
        X [I] = scan.nextLine();
        我++;
        }
    scan.close();
}

主要

 公共静态无效的主要(字串[] args){ 尝试{    //获取文件名
的System.out.println(输入你想搜索的文本文件的名称);
    扫描仪KB =新的扫描仪(System.in);
    字符串文件名= kb.nextLine();
    字符串TXT =.TXT;
    如果(!fileName.endsWith(TXT)){
        文件名= fileName.concat(TXT);
    }    文件输入=新的文件(文件名);//创建索引的第一部分
的System.out.println(创建vocabArray);
INT NUM_WORDS = getNumberOfWords(输入);
//System.out.println(NUM_WORDS);
的String [] = wordArray新的String [NUM_WORDS]
readInWords(输入,wordArray);
Arrays.sort(wordArray);
INT NUM_DISTINCT_WORDS = getNumOfDistinctWords(输入,wordArray);
的String [] = vocabArray新的String [NUM_DISTINCT_WORDS]
readInDistinctWords(wordArray,vocabArray);
的System.out.println(完成创建vocabArray);的System.out.println(创建concordanceArray);
INT NUM_LINES = getNumberOfLines(输入);
的String [] = concordanceArray新的String [NUM_LINES]
readInLines(输入,concordanceArray);
的System.out.println(完成创建concordanceArray);的System.out.println(创建invertedIndex);
INT [] [] invertedIndex =新INT [NUM_DISTINCT_WORDS] [10];
INT [] = wordCountArray新INT [NUM_DISTINCT_WORDS]
INT LINENUM = 0;
    而(LINENUM< concordanceArray.length){
        扫描程序扫描=新的扫描仪(concordanceArray [LINENUM]);
        而(scan.hasNext()){
            INT wordPos = Arrays.binarySearch(vocabArray,scan.next());
            wordCountArray [wordPos] + = 1;
            的for(int i = 0; I< invertedIndex.length;我++){
            对于(INT J = 0; J< invertedIndex [I]。长度;我++){
            如果(invertedIndex [I] [J] == 0){
            invertedIndex [I] [J] = LINENUM;
            打破;
            }}}
            }
        LINENUM ++;
        }
的System.out.println(完成创建invertedIndex);}    赶上(FileNotFoundException异常除外){
    的System.out.println(找不到文件);
}
} //主

} //类


解决方案

 为(INT J = 0; J< invertedIndex [I]。长度;我++){

也许应该是

  J ++

不是

 我++

您修复后更新。

这意味着 Arrays.binarySearch(vocabArray,scan.next())未找到正在搜索的项目。你不能假设vocabArray有您正在搜索的项目。您将需要添加一个如果。(...℃下)为呼叫的binarySearch

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

解决方案

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.

这篇关于Cannnot查找和QUOT;数组出界异常&QUOT的; Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆