I'm trying to read a integer number from the keyboard and I have the following code
String line;
int val = 0;
BufferedReader is = new BufferedReader (new InputStreamReader (System.in));
System.out.println("Type a number: ");
line = is.readLine();
val = Integer.parseInt(line);
System.out.println("I read this number "+ val);
But when I run the program, the console displays "Type a number: " and before letting me even type a number, it gives me all kinds of errors, complaining that I'm trying to parse an empty string to an integer. Can anyone tell me what's wrong?
Question
NtOrZor
I'm trying to read a integer number from the keyboard and I have the following code
String line; int val = 0; BufferedReader is = new BufferedReader (new InputStreamReader (System.in)); System.out.println("Type a number: "); line = is.readLine(); val = Integer.parseInt(line); System.out.println("I read this number "+ val);But when I run the program, the console displays "Type a number: " and before letting me even type a number, it gives me all kinds of errors, complaining that I'm trying to parse an empty string to an integer. Can anyone tell me what's wrong?
Link to comment
Share on other sites
16 answers to this question
Recommended Posts