1

Hello i am trying to get some java to run however i keep getting an error message here's the message: unreported exception IOException; must be caught or declared to be thrown myName = in.readLine();

 import java.io.*; public class While{ public static void main(String []args){ int num = 0; while (num != 999){ System.out.println("Enter a number:"); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); num = Integer.parseInt(in.readLine()); System.out.println("You typed: " + num); } System.out.println("999 entered, so the loop has ended."); } } 

Just going to be straight out, i have not used java and this is my first time ever using it, i was asked by my boss if i can take a look at it so far i've been able to do everything but i cannot fix this error message, any and all help welcome.

1 Answer 1

7

Surround the code with a try-catch statement and move the BufferedReader initialization before the while loop. Also, make sure to always close the resources after using them.

public static void main(String []args) { int num = 0; BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(System.in)); while (num != 999){ System.out.println("Enter a number:"); num = Integer.parseInt(in.readLine()); System.out.println("You typed: " + num); } } catch (Exception e) { //handle your exception, probably with a message //showing a basic example System.out.println("Error while reading the data."); e.printStacktrace(System.in); } finally { if (in != null) { try { in.close(); } catch (Exception e) { System.out.println("Problem while closing the reader."); e.printStacktrace(System.in); } } } System.out.println("999 entered, so the loop has ended."); } 

If you're using Java 7, then you can leverage all the code by using try with resources statement:

public static void main(String []args) { int num = 0; try(BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) { while (num != 999){ System.out.println("Enter a number:"); num = Integer.parseInt(in.readLine()); System.out.println("You typed: " + num); } } catch (Exception e) { //handle your exception, probably with a message //showing a basic example System.out.println("Error while reading the data."); e.printStacktrace(System.in); } System.out.println("999 entered, so the loop has ended."); } 
Sign up to request clarification or add additional context in comments.

11 Comments

empty catch is very very very bad
@OliverWatkins answer updated to show how to handle an exception.
In general when I have determined that an exception is (as far as I know) not going to happen I rethrow a RuntimeException, so if the Exception really does happen at least the application falls over with a decent exception rather than doing something "suprising". (NB; i'm not the downvoter either)
I get 9 errors with this all class, interface, or enum expected I'll work on getting this error fixed, thanks for the help, much appreciated.
@LuiggiMendoza in.close() requires also a try/catch.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.