java.lang.NumberFormatException: For input string: " "
Asked Answered
L

6

5

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.

Lowland answered 31/3, 2011 at 22:32 Comment(1)
Have you also got "" in your input (not space but null or empty strings)?Nuncupative
R
8

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.

Racialism answered 31/3, 2011 at 22:37 Comment(0)
G
1

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

Gusella answered 31/3, 2011 at 22:37 Comment(0)
D
1

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.

Desex answered 31/3, 2011 at 22:37 Comment(1)
yes i was thinking to catch the numberformatexception. and set it to equal to 0Lowland
P
1

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

Prude answered 1/4, 2011 at 0:44 Comment(0)
A
0

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.

Acquittance answered 31/3, 2011 at 22:43 Comment(0)
U
0

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() ; 
}
University answered 9/9, 2019 at 7:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.