【发布时间】: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 语法突出显示不显示行号。最后一个建议:拿起一个调试器来仔细检查你的代码是否在做你认为应该做的事情。