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.

1
  • Have you also got "" in your input (not space but null or empty strings)? Commented Mar 31, 2011 at 22:33

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

yes i was thinking to catch the numberformatexception. and set it to equal to 0
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

Comments

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

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.