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?
NumberFormatExceptionspecifies a string that causes an exception to appear. Analyze it and add stacktrace to the question. - Regent