There is a text file that contains integers, each with a new line:

num.txt

32 4 8 3 2 

When executing the following code:

 public static void main(String[] args) throws IOException { BufferedReader reader1 = new BufferedReader(new InputStreamReader(System.in)); String name = reader1.readLine(); BufferedReader reader = new BufferedReader(new FileReader(name)); while (reader.ready()) { buf = reader.readLine(); System.out.println(Integer.parseInt(buf)); } } 

Error NumberFormatException .
When working with the console, everything works fine.

It reads like what you need:

 public static void main(String[] args) throws IOException { BufferedReader reader1 = new BufferedReader(new InputStreamReader(System.in)); String name = reader1.readLine(); BufferedReader reader = new BufferedReader(new FileReader(name)); String buf = ""; try { while (reader.ready()) { buf = reader.readLine(); System.out.println(Integer.parseInt(buf)); } } catch (Exception e) { System.out.println("Мы считали: " + buf); } } 

We counted: 32

Where is the mistake?

  • The stacktrace for NumberFormatException specifies a string that causes an exception to appear. Analyze it and add stacktrace to the question. - Regent
  • Exception in thread "java.lang.NumberFormatException: For input string:" 32 ". I do not know how to add this to the question - Denis
  • At the bottom of the question, just below the tags, there is a "Edit" button. Click on it and add the whole stacktrace to the end of the question text. - Regent

2 answers 2

If the trim method does not cope with the task (in particular, it removes only characters with codes from 0 to 32 inclusive and only from the beginning and end of the line), then replaceAll can replaceAll :

 buf = buf.replaceAll("[^0-9]", ""); 

Which will remove from the string all characters that are not digits.

To find out which characters are contained in the string, you can use this method:

 private static void displayChars(String str) { for (int i = 0; i < str.length(); i++) { System.out.print((int)str.charAt(i) + " "); } } 

If System.out.println("Мы считали: " + buf); replace with displayChars(buf); , it should be clear what kind of symbols "spoil life".


This symbol could be 65279 (that is, BOM), which is at the very beginning of the file.

  • Thank! replaceAll () helped. Translated the string into an array of characters and output the character codes of the array. It turned out that there is a symbol 65279, "This symbol is nothing but a BOM (Byte Order Mark) which is located at the beginning of the file with UTF-8 encoding, and possibly some other Unicode encodings. Used to indicate byte order. Well, to show 16-bit or 32-bit numbers are used " - Denis
  • @Denis to health. Yes, it was the most likely option. However, in the answer I did not write about him, because I have this symbol (I don’t know for what reason) visible in Мы считали: 32 as a dot without 3 . - Regent

The error is most likely due to the fact that there is a space or some other similar character after the number. There is a sentence: after reading the string, call the trim() method for it. This method removes spaces at the beginning and end of a line.

Then the parsing will look like this:

 Integer.parseInt(buf.trim()) 
  • trim () didn't help. When outputting to the console, buf is output without spaces and hyphenations - Denis