开发者

java.lang.NumberFormatException: For input string: " "

When I try to do this:

total = Integer.parseInt(dataValues_fluid[i]) + total;

It will give me an err开发者_Python百科or 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.


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.


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.


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


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


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.


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() ; 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜