gpt4 book ai didi

java - 初始化字符数组时出现精度损失错误

转载 作者:行者123 更新时间:2023-11-30 07:51:10 25 4
gpt4 key购买 nike

我正在构建一个程序,我必须逐字扫描文档,解析这些单词以删除非法字符,然后将每个单词分配给 HashMap 。为此,我将单词中的每个字母添加到字符数组列表中,然后使用增强的 for 循环将该数组列表转换为数组,然后将该数组转换为字符串。问题是,每当我尝试使用数组列表的大小来初始化字符数组时,它都会抛出可能的精度损失错误,并且似乎希望我以字符而不是整数的形式输入值。我该如何解决这个问题并使我的阵列正常工作?请帮忙!

  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
}

最佳答案

您初始化 char 数组的方式错误。

char[] word = new char[LENGTH];

大括号用于数组的内容。

关于java - 初始化字符数组时出现精度损失错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33333175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com