Java 为什么在将字符串数组转换为int数组时会出现NumberFormatException?
在IntelliJ上运行代码时,我没有收到错误。然而,当我试图为我正在处理的任务提交代码时,我得到了两个测试用例的NFE。我删除了所有代码,只让下面的代码在测试用例中运行。这里的某个地方必须有一个Java 为什么在将字符串数组转换为int数组时会出现NumberFormatException?,java,regex,string,numberformatexception,parseint,Java,Regex,String,Numberformatexception,Parseint,在IntelliJ上运行代码时,我没有收到错误。然而,当我试图为我正在处理的任务提交代码时,我得到了两个测试用例的NFE。我删除了所有代码,只让下面的代码在测试用例中运行。这里的某个地方必须有一个NumberFormatException public class Search { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc
NumberFormatException
public class Search {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
int[] list = new int[n];
String [] tokens = sc.nextLine().trim().split(" ");
for (int i=0; i<tokens.length;i++){
list[i]=Integer.parseInt(tokens[i]);
}
}
}
并将其交给测试用例。IntelliJ将传递false,后跟一个整数数组,测试用例输出:
真的
[]
.
因此,你们都是对的,我只是错误地假设我的测试用例中的输入与我在作业中给出的示例输入相似
编辑2:
好的!
我想出来了。测试用例的输入与我的测试输入中的格式不同,看起来有点像这样:
1088911111171186298321
2
8 13 我假设我包含的sc.nextLine()跳过了列表中需要的整数。 因此,实际的问题不是额外的空间或任何东西,而是我通过使用sc.nextLine()超越了我想要的输入。 答案给了我我需要的暗示,即使我不认为这是故意的,但它来自安德洛尼卡。
无论如何都要感谢大家。如果您知道,将有一个整数作为输入,您不必担心解析,为什么不使用它呢
int input = sc.nextInt();
在您的解决方案中,您必须执行以下操作:
Arrays.stream(sc.nextLine().trim().split(" ")).filter(s -> !s.matches("\\s")).toArray(String[]::new);
\\ or simplier
sc.nextLine().trim().split("\\s+")
如果您知道将有一个整数作为输入,并且您不担心解析,为什么不使用它呢
int input = sc.nextInt();
在您的解决方案中,您必须执行以下操作:
Arrays.stream(sc.nextLine().trim().split(" ")).filter(s -> !s.matches("\\s")).toArray(String[]::new);
\\ or simplier
sc.nextLine().trim().split("\\s+")
这可能是因为数字之间有额外的一堆空格 比如说,
9 8 7 9 1
^^ ^^
*Note: You have more than one spaces here.
这就是您的阵列在拆分后的外观
tokens = {"9", "", "", "", "8", "7", "9", "", "", "", "1"}
由于额外的空格,上面的将抛出NumberFormatException
您可以再次尝试修剪内容
int i = 0;
for (String token : tokens){
token = token.trim();
if (!"".equals(token)) {
list[i++] = Integer.parseInt(token);
}
}
这可能是因为数字之间有额外的一堆空格
比如说,
9 8 7 9 1
^^ ^^
*Note: You have more than one spaces here.
这就是您的阵列在拆分后的外观
tokens = {"9", "", "", "", "8", "7", "9", "", "", "", "1"}
由于额外的空格,上面的将抛出NumberFormatException
您可以再次尝试修剪内容
int i = 0;
for (String token : tokens){
token = token.trim();
if (!"".equals(token)) {
list[i++] = Integer.parseInt(token);
}
}
有许多可能的原因:
代币中有一个非数字——例如9 1!3 x 3
令牌被多个空格分割——例如9 3
您应该能够通过数字格式异常的文本来判断。例如,在多个空间的情况下,您会得到:
线程“main”java.lang.NumberFormatException中的异常:用于输入字符串:“”
对于非数字(如“a”),您将得到:
线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“a”
当然,有很多可能的解决方案,这取决于当您遇到无效输入时您想做什么(您是否忽略它?抛出特殊异常?尝试去除非数字?)
当您知道输入由空格分隔,但不知道有多少空格时,可以使用正则表达式在split
命令中针对多个空格:
str.split("\\s+"); // splits on one or more whitespace including tabs, newlines, etc.
然后,要处理令牌列表中的非数字,可以在for循环中添加一个检查:
for(int i = 0; i < tokens.length; i++) {
if(tokens[i].matches("\\d+")) {
list[i] = Integer.parseInt(tokens[i]);
} else {
// Handle error case for non-digit input
}
}
for(int i=0;i
可能的原因有很多:
代币中有一个非数字——例如9 1!3 x 3
令牌被多个空格分割——例如9 3
您应该能够通过数字格式异常的文本来判断。例如,在多个空间的情况下,您会得到:
线程“main”java.lang.NumberFormatException中的异常:用于输入字符串:“”
对于非数字(如“a”),您将得到:
线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“a”
当然,有很多可能的解决方案,这取决于当您遇到无效输入时您想做什么(您是否忽略它?抛出特殊异常?尝试去除非数字?)
当您知道输入由空格分隔,但不知道有多少空格时,可以使用正则表达式在split
命令中针对多个空格:
str.split("\\s+"); // splits on one or more whitespace including tabs, newlines, etc.
然后,要处理令牌列表中的非数字,可以在for循环中添加一个检查:
for(int i = 0; i < tokens.length; i++) {
if(tokens[i].matches("\\d+")) {
list[i] = Integer.parseInt(tokens[i]);
} else {
// Handle error case for non-digit input
}
}
for(int i=0;i
请将您的代码修改为:
public class Example {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array : ");
int n = sc.nextInt();
sc.nextLine();
int[] list = new int[n];
System.out.println("Enter a string : ");
/** This regex will work for string having more than one space. */
String trimmedToken = sc.nextLine().replaceAll("\\s+", " ");
String[] tokens = trimmedToken.split(" ");
for (int i = 0; i < tokens.length; i++) {
list[i] = Integer.parseInt(tokens[i]);
System.out.println(list[i]);
}
sc.close();
}
}
输出:
1
2
3
4
5
请将您的代码修改为:
public class Example {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array : ");
int n = sc.nextInt();
sc.nextLine();
int[] list = new int[n];
System.out.println("Enter a string : ");
/** This regex will work for string having more than one space. */
String trimmedToken = sc.nextLine().replaceAll("\\s+", " ");
String[] tokens = trimmedToken.split(" ");
for (int i = 0; i < tokens.length; i++) {
list[i] = Integer.parseInt(tokens[i]);
System.out.println(list[i]);
}
sc.close();
}
}
输出:
1
2
3
4
5
在解析之前,请尝试记录令牌[i]
。你会知道,是什么导致了NFE。是的,令牌中有一个非整数,它导致了NFE。可能有某种测试用例正在针对以非数字形式通过的代码运行。请使用此split(\\s+”)
而不是split(“”
。这也是我提出的解释。但是请看一下我给出的输入:108891211111171186298321这些是我输入的前两行。对我来说,这看起来没有任何非整数标记。我还尝试打印出我的令牌数组。结果如下:[8、8、9、12、110、111、117、186、298、321]也没有非整数成员上述输入与您发布的代码配合良好。这意味着这不是全部代码(因此问题出在其他地方),或者这不是您传递的输入。请在解析之前尝试记录tokens[i]
。你会知道,是什么导致了NFE。是的,令牌中有一个非整数,它导致了NFE。可能有某种测试用例t