[英]NumberFormatException: For input string: “” while parsing
I wished to turn a string of single digit integers into an Integer
ArrayList, and I'm getting a very odd exception. 我希望将一个整数位数的字符串转换为Integer
ArrayList,但我遇到了一个非常奇怪的异常。 Here's the sample code: 这是示例代码:
Scanner test = new Scanner(System.in);
String[] temp = null;
String line = null;
ArrayList<Integer> artemp = new ArrayList<>();
while(test.hasNext()) {
line = test.next();
temp = line.split("");
for(int i = 0; i < temp.length; i++)
artemp.add(Integer.parseInt(temp[i]));
for(int i : artemp) System.out.println(i);
}
It is basically supposed to read the string of ints from stdin, put them in an arraylist as primitives, and then print them out. 基本上应该从stdin读取int字符串,将它们作为原始变量放入arraylist中,然后打印出来。 Of course, this is just a small test case to which I narrowed down my larger error. 当然,这只是一个很小的测试用例,我在其中缩小了较大的错误。
The exception this raises is the following: 引发的异常如下:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
As far as I can see, the following should take place: 据我所知,应该发生以下情况:
line
takes in stdin line
进入标准输入 temp
gets filled with each of the individual single-digit values (split("") splits after every character) temp
被每个单个的数字值填充(split(“”)在每个字符后拆分) String
) is parsed as an int
and added to artemp
将每个数字(存储为String
)解析为一个int
并将其添加到artemp
What exactly is going wrong here? 这到底是怎么了? If I print out temp
with the following code: 如果我用以下代码打印出temp
:
for(String s : temp) System.out.println(s);
Then the digits get printed successfully (as Strings), and I there are no extra characters. 然后数字成功打印(作为字符串),并且我没有多余的字符。 How exactly could parseInt(temp[i])
be getting applied to the string ""
? 如何将parseInt(temp[i])
应用于字符串""
?
First of all I'm suspecting your split, 首先,我怀疑你会分裂,
temp = line.split("");
If you want to split with space ,it should be 如果您想与空间分开,它应该是
temp = line.split(" ");
And if you want with ""
only,then here is the cause. 如果只想使用""
,那么原因就在这里。
There is nothing to do with conversion. 与转换无关。
There are some empty strings in you array
. array
有一些空字符串。
So exception while converting empty string in to integer. 因此,将空字符串转换为整数时会出现异常。
here 这里
artemp.add(Integer.parseInt(temp[i]));
What you can do is,check the data before parsing. 您可以做的是,在解析之前检查数据。
if(temp[i]!=null && !temp[i].isEmpty()){
artemp.add(Integer.parseInt(temp[i]));
}
您的split(“”)为null,您应该编写split(“”)(带引号的双引号)
Rather than using split, I'd think using the string itself will be better. 与使用split相比,我认为使用字符串本身会更好。 Try this. 尝试这个。
Scanner test = new Scanner(System.in);
String[] temp = null;
String line = null;
ArrayList<Integer> artemp = new ArrayList<>();
while(test.hasNext()) {
line = test.next();
for(int i = 0; i < line.length; i++)
artemp.add(Integer.parseInt(""+line.charAt(i));
for(int i : artemp) System.out.println(i);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.