• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

input

 
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
 
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
Boost this thread!