【问题标题】:Program throwing java.lang.StringIndexOutOfBounds exception for unknown reason程序因未知原因抛出 java.lang.StringIndexOutOfBounds 异常
【发布时间】:2015-10-25 20:04:21
【问题描述】:

我现在在运行 HashMap 程序时遇到问题。它可以编译,但运行它会抛出一个与我在第 45 行使用charAt 相关的java.util.StringIndexOutOfBoundsException

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

} }求助!

【问题讨论】:

  • 您的counter-1
  • 你应该考虑在下次发布之前清理你的代码,有一些杂散的右括号,而且似乎有一半的代码示例是不必要的,因为它中途失败了。此外,在 cmets 中标记异常/堆栈跟踪引用的行也很有帮助,因为 SO 语法突出显示不显示行号。最后一个建议:拿起一个调试器来仔细检查你的代码是否在做你认为应该做的事情。

标签: java arrays hashmap


【解决方案1】:

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() - 每次返回一个字符串,counter0length-1 依次递增,但characterizer.next().charAt(counter) 不能工作,因为每个匹配的字符串都是尺寸 1 始终。

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

【讨论】:

  • 是的,但我不知道为什么会这样。我认为它不应该变成负值,我不知道为什么会这样。
  • 鉴于您粘贴到问题中的这段代码,我们无法帮助您解决该问题。该变量在进入您的 while 循环之前获取其 -1 值。
  • 我应该发布其余的吗?
  • 那会很有帮助,是的。
  • 它不允许我发表这么大的评论。
猜你喜欢
  • 2017-08-09
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 2013-11-22
  • 1970-01-01
  • 2013-04-09
  • 1970-01-01
相关资源
最近更新 更多