aspose file tools*
The moose likes Beginning Java and the fly likes Problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem." Watch "Problem." New topic
Author

Problem.

Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
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 ]

Stephen Foy - Microsoft Application Development Consultant
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780

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.


There is no emoticon for what I am feeling!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14428
    
  23

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()).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Justin bob
Greenhorn

Joined: Nov 07, 2005
Posts: 3
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Aug 05, 2005
Posts: 3742
    
  16
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


Joanne
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Yes, that code needs help -- it doesn't even compile...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem.