现在我在运行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);
}
}
}}}请救命!
发布于 2015-10-26 04:09:43
for an unknown reason -原因实际上已经很清楚地告诉你了:
StringIndexOutOfBoundsException:字符串索引超出范围:java.lang.String.charAt处的-1 (未知源)
在某些时候,“字符串索引”是-1,这是“超出范围”的。唯一使用字符串“索引”的地方是下面这段:
characterizer.next().charAt(counter);字符串索引的适当“范围”通常是从0到string.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)。
https://stackoverflow.com/questions/33334484
复制相似问题