File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem.

 
Stephen Foy
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Been trying to fix this all night. Cant get it to work.



When pressing q it wont quit.


[ November 04, 2005: Message edited by: ste fing ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here I assumed that if the user entered several ints on one line,
I would read the first and discard the rest. You may want to read
them all.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are reading from the Scanner object using the method nextInt(). Do you know what nextInt() does when the user enters something else than a valid number, such as the letter 'q'? From the API documentation:

Scans the next token of the input as an int. This method will throw InputMismatchException if the next token cannot be translated into a valid int value as described below.

So, an InputMismatchException is thrown when the user enters 'q'. If you don't want this, use a method like nextLine() instead, and check if it contains 'q' before converting it to an integer yourself (which you can do with Integer.parseInt()).
 
Justin bob
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner in = new Scanner(System.in);
do{
System.out.print("Enter an integer: ");
String answer = in.nextLine;
}while(answer != "q")
System.out.prinln("Application Terminated");


i believe this would be much easier....
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper de Jong:
So, an InputMismatchException is thrown when the user enters 'q'. If you don't want this, use a method like nextLine() instead, and check if it contains 'q' before converting it to an integer yourself (which you can do with Integer.parseInt()).


Whilst this is true, one of the annoying things about parsing methods like
Integer.parseInt is that you can't look before you leap -- if the next token
is not an int you have to handle this in a catch block instead of in normal code
flow. On the other hand, Scanner has methods like hasNextInt which is true
iff a call to nextInt would succeed at this point, so that you can encorporate
this test into normal code flow. Try it!
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Justin bob:
Scanner in = new Scanner(System.in);
do{
System.out.print("Enter an integer: ");
String answer = in.nextLine;
}while(answer != "q")
System.out.prinln("Application Terminated");


i believe this would be much easier....


Use equals() to compare Strings
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that code needs help -- it doesn't even compile...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic