Milan Todorovic Milan Todorovic - 1 year ago 114
Java Question

java.lang.NumberFormatException for input string "1"

So, I have an issue that really bothers me. I have a simple parser that I made in java. Here is the piece of relevant code:

while( (line = br.readLine())!=null)
String splitted[] = line.split(SPLITTER);
int docNum = Integer.parseInt(splitted[0].trim());
//do something

Input file is CSV file, the first entry of the file being an integer. When I start parsing, I immidiately get this exception:

Exception in thread "main" java.lang.NumberFormatException: For input string: "1"
at java.lang.NumberFormatException.forInputString(
at java.lang.Integer.parseInt(
at java.lang.Integer.parseInt(
at dipl.parser.TableParser.parse(
at dipl.parser.DocumentParser.main(

I checked the file, it indeed has 1 as it's first value (no other characters are in that field), but I still get the message. I think that it may be because of file encoding: it is UTF-8, with Unix endlines. And the program is run on Ubuntu 14.04. Any suggestions where to look for the problem are welcome.

Answer Source

You have a BOM in front of that number; if I copy what looks like "1" in your question and paste it into vim, I see that you have a FE FF (e.g., an UTF-8 BOM) in front of it.

So that's the issue, consume the UTF-8 BOM at the beginning of the file by using a UTF-8 reader.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download