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

input

 
Bob Gucco
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bob,
How about using a loop until the user has typed a string:

Regards,
Manfred.
 
Bob Gucco
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Bob Gucco
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic