wood burning stoves 2.0*
The moose likes Java in General and the fly likes input Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "input" Watch "input" New topic
Author

input

Bob Gucco
Greenhorn

Joined: Jan 18, 2001
Posts: 16
How can I accomplish ignoring "enter" key.
Suppose by initializing of my variables "n1, n2, desc" after
-
-
System.out.print("Number 1 :");
-
-
instead of putting some data first
I hit "enter" by accident - I would like my program to continue without throwing an exception. What can I do about it?
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
System.out.print("Number 1 :");
String n1 = input.readLine();
int number1 = Integer.parseInt(n1);
System.out.print("Number 2 :");
String n2 = input.readLine();
int number2 = Integer.parseInt(n2);
System.out.print("Description :");
String desc = input.readLine();
Thanks
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Bob,
How about using a loop until the user has typed a string:

Regards,
Manfred.
Bob Gucco
Greenhorn

Joined: Jan 18, 2001
Posts: 16
Thanks
Bob Gucco
Greenhorn

Joined: Jan 18, 2001
Posts: 16
How about if I want by clicking lets say "M" do something.
How can interpretor find out if it is "M" or "m"?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
How about just checking for it?
if( s.equals( "M" ) )
{
// Upper case M
}
else if( s.equals( "m" ) )
{
// Lower case m
}
Manfred.
Bob Gucco
Greenhorn

Joined: Jan 18, 2001
Posts: 16
Thanks
 
jQuery in Action, 2nd edition
 
subject: input