java.lang.NumberFormatException:对于输入字符串:“ ”

发布于 2024-10-28 16:38:08 字数 472 浏览 5 评论 0原文

当我尝试执行此操作时:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猫弦 2024-11-04 16:38:08

我假设 dataValues_fluid[] 是一个 String 数组。如果是这种情况,则无法使用 == 比较运算符 - 您需要使用 if(dataValues_fluid[i].equals(" "))

因此,您的 parseInt() 正在尝试解析空格字符,这会导致您的 NumberFormatException

I'm assuming that dataValues_fluid[] is an array of Strings. If this is the case, you can't use the == comparison operator - you need to use if(dataValues_fluid[i].equals(" ")).

So your parseInt() is attempting to parse the space character, which results in your NumberFormatException.

扎心 2024-11-04 16:38:08

您需要使用 equals 来比较字符串:

if(dataValues_fluid[i].equals(" ")){dataValues_fluid[i]="0";}

不过,一般来说,更优雅的方法是捕获 NumberFormatException 并将该值设置为 0(在这种情况下)。

You need to compare strings using equals:

if(dataValues_fluid[i].equals(" ")){dataValues_fluid[i]="0";}

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.

丑疤怪 2024-11-04 16:38:08

我认为 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

以歌曲疗慰 2024-11-04 16:38:08

不要使用 == 将字符串与 " " 进行比较,而是这样做:

if (" ".equals(dataValues_fluid[i]) { dataValues_fluid[i] = "0"; }

注意使用 ==之间的区别。 equals() 与字符串。 == 不适用于比较 Java 中的字符串 - 您必须使用 .equals() 方法。

http://leepoint.net/notes-java/data/expressions/22compareobjects。 html

Rather than using == to compare the string to " ", do this instead:

if (" ".equals(dataValues_fluid[i]) { dataValues_fluid[i] = "0"; }

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

不喜欢何必死缠烂打 2024-11-04 16:38:08

您可以将添加到总计的行包装在 try/catch 块中,因此任何无效数字都将被忽略(如果您想了解错误,可以记录错误)。

try {
    total += Integer.parseInt(dataValues_fluid[i]);
} catch (NumberFormatException ignored) {}

这样,您将忽略任何非数字值,而不仅仅是空格,并且您的程序将执行良好(为了安全起见,请记录该异常或在发生异常时执行一些双重检查)。

如果数字无效,则不会添加任何内容到您的总数中,因此您的程序将按预期执行。

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).

try {
    total += Integer.parseInt(dataValues_fluid[i]);
} catch (NumberFormatException ignored) {}

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.

淡紫姑娘! 2024-11-04 16:38:08

您可以使用此代码。
问题是验证你的条件的条件。
所以我使用三角验证来优化一些内存。

int total = 0;
for(int i=0; i<counter5; i++){
total += Integer.parseInt(dataValues_fluid[i].equals(" ") ? "0" : dataValues_fluid[i]) ;
}

在该方法中,您可以使用“抛出 NumberFormatException ”,否则

try {
  int total = 0;
  for(int i=0; i<counter5; i++){
   total += Integer.parseInt(dataValues_fluid[i].equals(" ") ? "0":dataValues_fluid[i]);
  }
}catch(NumberFormatException e){
  e.printStackTrace() ; 
}

You can use this code.
The problem was the condition to verify your condition.
So I use a tringle verification to optimize some memory.

int total = 0;
for(int i=0; i<counter5; i++){
total += Integer.parseInt(dataValues_fluid[i].equals(" ") ? "0" : dataValues_fluid[i]) ;
}

in the method u can use a "throws NumberFormatException " else

try {
  int total = 0;
  for(int i=0; i<counter5; i++){
   total += Integer.parseInt(dataValues_fluid[i].equals(" ") ? "0":dataValues_fluid[i]);
  }
}catch(NumberFormatException e){
  e.printStackTrace() ; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文