91StarSky
91StarSky

Reputation: 482

Baffling incident of number format exception

I am trying to parse some integers in loop. Some of them are throwing NumberFormatException weirdly because it shows error :-

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

The line that throws error :-

    closingPoints += Integer.parseInt(tokens[i + 1]);

Upvotes: 0

Views: 74

Answers (2)

Ioannis Barakos
Ioannis Barakos

Reputation: 1369

You have non-printable / Non-ASCII characters on your String. You can try to remove them using a regular expression. For example

closingPoints += Integer.parseInt(tokens[i + 1].replaceAll( "[^\\x00-\\x7F]", "" ));

What the above regex does is to replace all characters that are not (^) between the hexadecimal range of 00-7F (0-127) which is the range of ASCII characters. If a character on that range is found, it replaces it with empty String.

Upvotes: 3

pyropunk51
pyropunk51

Reputation: 533

Check that you do not have:

  • spaces
  • invisible control characters
  • the 0 (zero) is not an O (Oh)
  • unicode characters

Upvotes: 2

Related Questions