程序因未知原因引发java.lang.StringIndexOutOfBounds异常

程序因未知原因引发java.lang.StringIndexOutOfBounds异常,java,arrays,hashmap,Java,Arrays,Hashmap,我现在在运行HashMap程序时遇到问题。它进行编译,但运行时会抛出一个java.util.StringIndexOutOfBoundsException,与我在第45行使用的charAt有关: import java.util.HashMap; import java.util.Map; import java.util.Iterator; import java.util.Set; import java.io.File; import java.io.FileNotFoundExcepti

我现在在运行HashMap程序时遇到问题。它进行编译,但运行时会抛出一个
java.util.StringIndexOutOfBoundsException
,与我在第45行使用的
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);
    }
  }
}
import java.util.HashMap;
导入java.util.Map;
导入java.util.Iterator;
导入java.util.Set;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.Scanner;
导入javax.swing.JFileChooser;
导入java.util.ArrayList;
//*这个程序输入一个文本文件,处理它,并将每个单词映射到一个哈希映射。最后,它输出一个所有*/
/*文件中唯一的单词(仅出现一次)以及最常用的五个单词*/
公共类HashMapLab
{
公共静态void main(字符串[]args)引发FileNotFoundException
{
//创建并初始化哈希映射
HashMap words=新的HashMap();
//允许用户选择文件并逐字输入
JFileChooser chooser=新的JFileChooser();
扫描仪输入=空;
if(chooser.showOpenDialog(null)=JFileChooser.APPROVE\u选项)
{
File selectedFile=chooser.getSelectedFile();
in=新扫描仪(已选择文件);
//这个冗长的循环逐个字符地处理每个单词
while(在.hasNext()中)
{
//输入选定文件中的下一个单词并将其转换为字符串
字符串输入=in.next();
//这个扫描器把单词一个字符一个字符地分解
扫描仪表征器=新扫描仪(输入);
characterizer.useDelimiter(“”);
int计数器=0;
ArrayList占位符=新的ArrayList();
while(计数器
}
}se help!

原因不明
-事实上,原因非常清楚:

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

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

characterizer.next().charAt(counter);
字符串索引的适当“范围”通常是从
0
string.length()-1

因此,从给定的错误中,您可以猜测由于某种原因,@Kayaman注意到,
计数器
变量是
-1


因相关变更而编辑:

代码
characterister.next().charAt(counter);
在您的例子中增加计数器,然后尝试从每个匹配字符串的位置获取字符,每次长度为1

为了改写这一点,
characterizer.next()
-每次返回一个1字符的字符串,
计数器
按顺序从
0
递增到
length-1
,但是
characterizer.next().charAt(counter)
,无法工作,因为每个匹配的字符串的大小始终为1


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

你的<代码>计数器<代码>是代码> -1 。在下一次发布之前,你应该考虑清理你的代码,有一些杂散的关闭括号,而且似乎一半的代码样例是不必要的,因为它在中途失败。他说语法highlighter不显示行号。最后一个建议是:使用调试器仔细检查你的代码是否在做你认为应该做的事情。是的,但我不知道为什么。我不认为它应该变为负值,我也不知道它为什么会这样做。考虑到你粘贴到问题中的代码片段,我们无法帮助您解决这个问题。变量在进入while循环之前获得其
-1
值。我是否应该发布其余部分?这将非常有用,是的。它不允许我发布如此大的注释。