我正在构建一个程序,我必须逐字扫描文档,解析这些单词以删除非法字符,然后将每个单词分配给哈希映射。为此,我将单词中的每个字母添加到字符arraylist中,然后使用增强的for循环将该arraylist转换为数组,然后将该数组转换为String。问题是,每当我尝试使用我的数组列表的大小初始化字符数组时,它会抛出一个可能的精度损失错误,并且似乎要我输入值作为字符而不是整数。如何解决此问题并使我的阵列工作?请帮忙!
while ( in .hasNext()) {
String input = in .next();
Scanner characterizer = new Scanner(input);
characterizer.useDelimiter("");
int counter = 0;
ArrayList < Character > placeHolder = new ArrayList < Character > ();
while (counter < input.length()) {
char character = characterizer.next().charAt(counter);
if (character != '(' && character != ')' && character != '.' && character != '-' && character != '$' && character != '?' & character != '!' && character != ';' && character != ':' && character != '"' && character != '&' && character != '#' && character != '*') {
placeHolder.add(character);
}
counter++;
}
final int LENGTH = placeHolder.size();
char[] word = new char[] {
LENGTH
};
//this code will used the enhanced for loop to copy the contents of placeHolder into word and then convert it to a string
}
答案 0 :(得分:1)
您正在以错误的方式初始化char数组。
char[] word = new char[LENGTH];
花括号用于数组的内容。