java.lang.NumberFormatException:对于输入字符串:“ ”
当我尝试执行此操作时:
total = Integer.parseInt(dataValues_fluid[i]) + Total;
它会给我一条错误消息,
java.lang.NumberFormatException: For input string: " "
其中一些值是“”。这就是为什么它给了我。但我尝试过
int total = 0;
for(int i=0; i<counter5; i++){
if(dataValues_fluid[i]==" "){dataValues_fluid[i]="0";}
total = Integer.parseInt(dataValues_fluid[i]) + total;
}
,仍然遇到相同的 java.lang.NumberFormatException 错误。
When I try to do this:
total = Integer.parseInt(dataValues_fluid[i]) + total;
It will give me an error message
java.lang.NumberFormatException: For input string: " "
Some of the values are " ". So thats why it gives me. But I tried
int total = 0;
for(int i=0; i<counter5; i++){
if(dataValues_fluid[i]==" "){dataValues_fluid[i]="0";}
total = Integer.parseInt(dataValues_fluid[i]) + total;
}
I still get the same java.lang.NumberFormatException
error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我假设 dataValues_fluid[] 是一个 String 数组。如果是这种情况,则无法使用
==
比较运算符 - 您需要使用if(dataValues_fluid[i].equals(" "))
。因此,您的
parseInt()
正在尝试解析空格字符,这会导致您的NumberFormatException
。I'm assuming that
dataValues_fluid[]
is an array ofString
s. If this is the case, you can't use the==
comparison operator - you need to useif(dataValues_fluid[i].equals(" "))
.So your
parseInt()
is attempting to parse the space character, which results in yourNumberFormatException
.您需要使用
equals
来比较字符串:不过,一般来说,更优雅的方法是捕获 NumberFormatException 并将该值设置为 0(在这种情况下)。
You need to compare strings using
equals
:In general though, the more elegant way to do it would be to catch the NumberFormatException and set the value to 0 in that case.
我认为 Andy White 的链接足以满足您的目的,但如果您需要另一个详细解释为什么“==”运算符不起作用以及有哪些替代方案(实际上有一些),请查看此链接:
http://www.devdaily.com/java/edu/qanda/pjqa00001.shtml
以下是其他同等方法的 API 文档信息:
http ://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#equals%28java.lang.Object%29
I think Andy White's link is enough for your purose, but if you needed another detailed explanation of why "==" operator doesn't work and what alternatives there are (actually there's a few), check this link out:
http://www.devdaily.com/java/edu/qanda/pjqa00001.shtml
Here's the API doc info on other equal methods:
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#equals%28java.lang.Object%29
不要使用
==
将字符串与" "
进行比较,而是这样做:注意使用
==
和之间的区别。 equals()
与字符串。==
不适用于比较 Java 中的字符串 - 您必须使用.equals()
方法。http://leepoint.net/notes-java/data/expressions/22compareobjects。 html
Rather than using
==
to compare the string to" "
, do this instead:Note the difference between using
==
and.equals()
with strings.==
does not work for comparing strings in Java - you have to use the.equals()
method.http://leepoint.net/notes-java/data/expressions/22compareobjects.html
您可以将添加到总计的行包装在 try/catch 块中,因此任何无效数字都将被忽略(如果您想了解错误,可以记录错误)。
这样,您将忽略任何非数字值,而不仅仅是空格,并且您的程序将执行良好(为了安全起见,请记录该异常或在发生异常时执行一些双重检查)。
如果数字无效,则不会添加任何内容到您的总数中,因此您的程序将按预期执行。
You can wrap the line that adds to your total in a try/catch block, so any invalid numbers will be ignored (you can log the error if you want to be informed about it).
This way you will ignore any non-numeric values, not only spaces and your program will perform well (to be also safe, log that exception or perform some double-checking when it occurs).
Nothing will be added to your total in case of invalid numbers, so your program will perform as expected.
您可以使用此代码。
问题是验证你的条件的条件。
所以我使用三角验证来优化一些内存。
在该方法中,您可以使用“抛出 NumberFormatException ”,否则
You can use this code.
The problem was the condition to verify your condition.
So I use a tringle verification to optimize some memory.
in the method u can use a "throws NumberFormatException " else