5

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
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Yunus Eren Güzel
  • 3,018
  • 11
  • 36
  • 63

1 Answers1

4

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.

jlordo
  • 37,490
  • 6
  • 58
  • 83