NumberFormatException on parsing int

So, I am making a file reader / writer that can access this file and save / read it. I have a problem reading from a file. The contents are integers, strings, and doubles, separated by the symbol "|" delimiters. I use StringTokenizer to split tokens and store them for each separate variable, but when I read integers, I get a NumberFormatException, although the string only contains int.

Here is the code:

FileReader fr = new FileReader(filename); BufferedReader buff = new BufferedReader(fr); String line; while ((line = buff.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, "|"); while (st.hasMoreElements()) { int Id = Integer.parseInt(st.nextToken()); String Name = st.nextToken(); double cordX = Double.parseDouble(st.nextToken()); double cordY = Double.parseDouble(st.nextToken()); } } 

Example file line:

 8502113|Aarau|47.391355|8.051251 

And the error:

 java.lang.NumberFormatException: For input string: "8502113" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.parseInt(Integer.java:615) at storage.FileUtilities.readCitiesFromFile(FileUtilities.java:63) at basics.Test.main(Test.java:16) 

Am I missing something? Is a StringTokenizer tampering with a string that I don't know?

EDIT: Here is the code that creates the file:

 FileWriter fw = new FileWriter(filename, !overwrite); // For FileWriter true = append, false = overwrite, so we flip the value. BufferedWriter buff = new BufferedWriter(fw); String coordConvertor; for (int i = 0; i <= cities.size() - 1; i++) { buff.write(Integer.toString(cities.get(i).getId())); buff.write("|"); buff.write(cities.get(i).getName()); buff.write("|"); coordConvertor = Double.toString(cities.get(i).getCoord().getX()); buff.write(coordConvertor); buff.write("|"); coordConvertor = Double.toString(cities.get(i).getCoord().getY()); buff.write(coordConvertor); buff.newLine(); 
+6
source share
1 answer

The String has hidden Unicode characters obtained using st.nextToken () . Use this code to remove them.

 int Id = Integer.parseInt(st.nextToken().replaceAll("\\p{C}", "")); String Name = st.nextToken().replaceAll("\\p{C}", ""); double cordX = Double.parseDouble(st.nextToken().replaceAll("\\p{C}", "")); double cordY = Double.parseDouble(st.nextToken().replaceAll("\\p{C}", "")); 
+3
source

All Articles