File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Simple input statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple input statement" Watch "Simple input statement" New topic

Simple input statement

Matt Hall

Joined: Feb 28, 2006
Posts: 24
The code:-

BufferedReader input = new BufferedReader
(new InputStreamReader(;
String inputString;

int contract;

System.out.print("Input desired contract: ");
inputString = input.readLine();
contract = Integer.parseInt(inputString);

I simply want to take a keyboard input and store it in the integer contract.

However this produces a compiler error of:- unreported exception; must be caught or declared to be thrown
inputString = input.readLine();

Can anybody help me with this, thanks.
vignesh hariharan
Ranch Hand

Joined: Jun 23, 2005
Posts: 77
yes.. you are supposed to catch the exception arised by using BufferedReader or DataInputStream. the exception arised is IOException.. jus do one thing.. when u declare ur main method do it like this

public static void main(String ar[])throws IOException
// your code here

what this acutally does is that when an IOException is arised this method throws the IOException to the JVM.. and thus JVM will take care of showing you the Output.

Matt Hall

Joined: Feb 28, 2006
Posts: 24
This works fine, thanks very much mate
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249


Or another thing you can do is surround the whole block with try-catch


This will also work definately......

I agree. Here's the link:
subject: Simple input statement
It's not a secret anymore!