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

razshan picture razshan · Apr 1, 2011 · Viewed 66.9k times · Source

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.

Answer

Kevin Lacquement picture Kevin Lacquement · Apr 1, 2011

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.