【问题标题】:Cannnot find "Array Out of Bounds Exception" Java找不到“数组越界异常”Java
【发布时间】:2012-04-13 15:25:05
【问题描述】:

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

当我现在运行这个程序时,我得到一个“数组索引超出范围异常”

线程“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

} //类

【问题讨论】:

  • 你从哪里得到异常?
  • 定义“找不到”;异常会准确地显示错误发生的位置。
  • 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: SearchEngine.main(SearchEngine.java:128) 中的 43
  • @user1302023 - 在第 128 行检查值为 43 的索引。
  • 查看我的编辑...最好提出不同的问题,而不是顺便问 cmets。

标签: java arrays exception


【解决方案1】:
for(int j = 0; j < invertedIndex[i].length; i++) {

应该是

j++

不是

i++

修复后更新。

这意味着Arrays.binarySearch(vocabArray, scan.next()) 没有找到正在搜索的项目。您不能假设 vocabArray 具有您正在搜索的项目。您需要为 binarySearch 调用添加一个if(... &lt; 0)

【讨论】:

  • 将它修复为 j++,现在我得到了这个:
  • 线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:SearchEngine.main(SearchEngine.java:126) 中的 -1
  • 但是如果你按照代码进行操作,它基本上会从 txt 文件中提取每个单词,并将其添加到 vocabArray。然后它从文件中取出每一行,将其添加到 concordanceArray。二进制搜索在 concordanceArray 的每一行中搜索每个单词并返回 vocabArray 中找到它的索引。所以理论上来说,vocabArray 应该有。
猜你喜欢
  • 1970-01-01
  • 2016-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-25
相关资源
最近更新 更多