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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With