Why can't the actual string of numbers read from the text be parsed using the Integer.valueOf () method in java?

Why can't the actual number string read from the text be parsed using the Integer.valueOf() method in java?

Exception :

 Exception in thread "main" java.lang.NumberFormatException: For input string: "11127" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) at java.lang.Integer.valueOf(Integer.java:766) at sharingBike.ReadTxt.readRecord(ReadTxt.java:91) at sharingBike.ReadTxt.main(ReadTxt.java:17) 

This is my code.

  File fileView = new File(filePath); BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileView), "UTF-8")); String line; int count = 0; while ((line = in.readLine()) != null) { String[] lins = line.split(";"); int value; value = Integer.valueOf(lins[0]);} 
+5
source share
1 answer

Here is the content of your line:

 System.out.println(Arrays.toString("11127".getBytes())); 

which outputs:

[- 17, -69, -65, 49, 49, 49, 50, 55]

The first three bytes are the UTF-8 specification .

You can fix this by removing non-digital lines from the first line (and use parseInt to return int instead of Integer ):

 int value = Integer.parseInt(lins[0].replaceAll("\\D", ""); 
+12
source

Source: https://habr.com/ru/post/1273208/


All Articles