程序因未知原因抛出java.lang.StringIndexOutOfBounds异常

时间:2015-10-25 20:04:21

标签: java arrays hashmap

我在运行HashMap程序时遇到问题。它会编译,但运行它会引发与我在第45行使用java.util.StringIndexOutOfBoundsException相关的charAt

import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import java.util.ArrayList;

//* This program inputs a text file, process it, and maps each word to a   hash map. At the end it outputs a list of all */
/* words in the file that are unique (occuring only once) and also the top five most commonly used words */


public class HashMapLab
{
  public static void main(String[] args) throws FileNotFoundException
{
//creates and initualizes a hash map
HashMap<String, Integer> words = new HashMap<String, Integer>();

//allows user to select the file and inputs it word by word
JFileChooser chooser = new JFileChooser();
Scanner in = null;
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
  File selectedFile = chooser.getSelectedFile();
  in = new Scanner(selectedFile);

  //This lengthy loop processes each word, character by character
  while (in.hasNext())
  {
    //The next word in the selected file is input and turned into a string
    String input = in.next();
    //And this scanner breaks the word up character by character
    Scanner characterizer = new Scanner(input);
    characterizer.useDelimiter("");
    int counter = 0;

    ArrayList<Character> placeHolder = new ArrayList<Character>();

    while (counter < input.length())
    {
      //This is the reason why. Each character is checked against a blacklist. Forbidden characters are discarded.
      char character = characterizer.next().charAt(counter);
      if (character != '(' && character != ')' && character != '.' && character != '-' && character != '$' 
         && character != '?' & character != '!' && character != ';' && character != ':' && character != '"' &&
         character != '&' && character != '#' && character != '*')
      {
        placeHolder.add(character);
      }
      counter++;
    }

    /*After adding all permitted characters to an arraylist of variable size, that array list is converted
     * here to a fixed length array. */
    final int LENGTH = placeHolder.size();
    char[] word = new char[LENGTH];

    int currentSize = 0;
    if (currentSize < word.length)
    {
      currentSize++;
      word[currentSize] = placeHolder.get(currentSize);
    }

    //Because it is an array, it can be simply converted into a string, now devoid of blacklisted characters.
    String finalWord = new String(word);

    /* This is what all that code was leading up to. finalWord should be a permissible word by now, contaning
     * no blacklisted characters. This loop checks to see if finalWord is in the hashmap yet. If it is
     * then the value of that word is incrimented. If not, it is added to the hashmap. This should allow
     * the entire document to be processed, producing a hashmap that contains each unique word in the document
     * along with the number of times that word is present. */
    if (words.containsKey(finalWord))
    {
      Integer I = words.get(finalWord);
      words.put(finalWord, I++);
    }
    else
    {
      words.put(finalWord, 1);
    }
  }
}

} 帮助!

1 个答案:

答案 0 :(得分:0)

for an unknown reason - 原因实际上非常明确地给了你:

  

StringIndexOutOfBoundsException:字符串索引超出范围:-1 at   java.lang.String.charAt(未知来源)

在某个时间点,“字符串索引”为-1,即“超出范围”。唯一使用字符串“索引”的地方是:

characterizer.next().charAt(counter);

字符串索引的正确“范围”通常是从0string.length()-1

因此,根据给定的错误,您可以猜测出于某种原因,正如@Kayaman所注意到的,counter变量是-1

由于问题的变化而编辑:

你的案例中的代码characterizer.next().charAt(counter);递增计数器,然后尝试从每个匹配的字符串中获取字符,每次匹配字符串长度为1。

要改写,characterizer.next() - 每次返回1个字符的字符串,counter按顺序从0递增到length-1,但是characterizer.next().charAt(counter),无法工作,因为每个匹配的字符串总是大小为1。

您可以删除特征描述符,将其保留在input.charAt(counter),或将charAt(counter)更改为charAt(0)