• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Problem.

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()).
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that code needs help -- it doesn't even compile...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic