简体   繁体   English

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

[英]Program throwing java.lang.StringIndexOutOfBounds exception for unknown reason

I'm now having a problem when running my HashMap program. 我现在在运行HashMap程序时遇到问题。 It compiles, but running it throws a java.util.StringIndexOutOfBoundsException related to my use of charAt on line 45: 它可以编译,但是运行它会抛出与我在第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);
    }
  }
}

} }se help! }}帮助!

for an unknown reason - the reason is actually given to you pretty clearly: for an unknown reason -实际上很清楚地向您提供了原因:

StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.charAt(Unknown Source) StringIndexOutOfBoundsException:字符串索引超出范围:java.lang.String.charAt(未知源)处为-1

At some point of time a "string index" is -1, which is "out of range". 在某个时间点,“字符串索引”为-1,即“超出范围”。 The only place that uses a "index" of a string, is this piece: 这是唯一使用字符串的“索引”的地方:

characterizer.next().charAt(counter);

And a proper "range" for the string index is usually from 0 to string.length()-1 . 字符串索引的适当“范围”通常是从0string.length()-1

Therefore, from the given error, you can guess that for some reason, as @Kayaman noticed, counter variable is -1 . 因此,从给定的错误中,您可以猜测由于某种原因,正如@Kayaman所注意到的, counter变量为-1


Edited because of change in question: 由于存在问题而进行了编辑:

The code characterizer.next().charAt(counter); 代码characterizer.next().charAt(counter); in your case increments the counter, and then tries to get the character from it's position from every matching string, which is of length 1 every time. 在您的情况下,计数器将递增,然后尝试从每个匹配的字符串(每次长度为1)从其位置获取字符。

To reword that, characterizer.next() - returns a 1 character string each time, counter is incremented in sequence from 0 to length-1 , but characterizer.next().charAt(counter) , can't work because each matched string is of size 1 always. 改写一下, characterizer.next() -每次返回1个字符串, counter按从0length-1顺序递增,但是characterizer.next().charAt(counter)无效,因为每个匹配的字符串始终为1。

You could either remove the characterizer at all, and leave it at input.charAt(counter) , or change charAt(counter) to charAt(0) . 您可以完全删除表征器,然后将其保留在input.charAt(counter) ,或将charAt(counter)更改为charAt(0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM