Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple input statement

 
Matt Hall
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works fine, thanks very much mate
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matt,

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

block.......

This will also work definately......

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic