File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simple input statement" Watch "Simple input statement" New topic
Author

Simple input statement

Matt Hall
Greenhorn

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

BufferedReader input = new BufferedReader
(new InputStreamReader(System.in));
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:-

Test.java:16: unreported exception java.io.IOException; 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.


Regards,
vignesh
Matt Hall
Greenhorn

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

Joined: Dec 27, 2005
Posts: 1249

Matt,

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

block.......

This will also work definately......

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple input statement
 
Similar Threads
How to run .java file from Runnable
Applets and filereading
whats wrong with my code
conversion
want to see my applet in browser -- reading a file