When using BufferedReader you need to take care of the exceptions that it can throw. In addition, the Integer.parseInt(String s) method may raise a NumberFormatException if the String you provide cannot be converted to Integer .
try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while ((thisLine = br.readLine()) != null) { System.out.println(thisLine); Integer parsed = Integer.parseInt(thisLine); System.out.println("Parsed integer = " + parsed); } } catch (IOException e) { System.err.println("Error: " + e); } catch (NumberFormatException e) { System.err.println("Invalid number"); }
source share