Click to See Complete Forum and Search --> : Reading an integer from the keyboard.


jason80
October 12th, 2001, 05:22 PM
Is there a simple way to read in an integer from the keyboard in a console application?

I tried the following code:


String line;
BufferedReader reader= new BufferedReader(new InputStreamReader(System.in));

line = reader.readLine();
int a = Integer.parseInt(line);




With this code, Visual Cafe gives me an error on the "reader.readLine()" line mentioning "unreported java.io.IOException. must be caught or declared to be thrown." I tried putting it in a try catch clause, but then it gives me other errors.

Is there another way to get keyboard input?

Thanks.

BennyL
October 24th, 2001, 02:43 AM
Take that

String input;
BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in));
try
{
System.out.println("Input: ");
input = keyboard.readLine();
}
catch(IOException e)
{
System.out.println("Error");
return;
}
System.out.println(input);