Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer.parseInt() throws NumberFormatException with UTF-8 files

I need to parse integers from contents of a file.

For testing my algorithms, when I give the contents of a file from a declared string

String test = "15 kuruş";

Integer.parseInt works fine. But when I read with Scanner class from a UTF-8 file it doesn't work and gives the exception

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

Note: I split the string to "15" and "kuruş" so the parseInt method takes only "15" as argument.

Sample code:

    satir = satir.trim();//15 kuruş
    StringTokenizer tokenizer = new StringTokenizer(satir," ");
    System.out.println(tokenizer.countTokens());//2
    String s = tokenizer.nextToken();
    int deger = Integer.parseInt(s);//where the exception was throwed
like image 377
Yunus Eren Güzel Avatar asked Oct 05 '22 18:10

Yunus Eren Güzel


1 Answers

Your UTF-8 File probably starts with a BOM, you have to read the File with the correct encoding or get rid of it manually.

So when your 15 is not preceeded with the BOM anymore, Integer.parseInt() will work.

like image 194
jlordo Avatar answered Oct 10 '22 03:10

jlordo