// STEP 3: The next two statements request the user to enter the value of n and then reads in their response as an int using the
// nextInt method of the Scanner class.
// The problem with this is that if the user enters something other than an int, we will have a runtime exception.
// After requesting the user enters n (first of the next two statements), write a while loop that repeats as long as the user
// enters something other than an int (recall that the hasNextInt method of the Scanner class will return true if the user
// enters an int and false otherwise. And recall that ! is Java's not operator. Inside the loop, you'll want to remove whatever they
// did enter with a call to the next method of the Scanner class. And you'll want to give them an error message indicating that they should try again.
// This loop should be immediately prior to the call to the nextInt method which has been done for you.

But obviously taht is not 100% correct. Any help would be great! Thanks

11-03-2009, 11:34 PM

travishein

it looks like the way hasNextInt() works is it tests to see if the next thing in the scanner is an int, if it is not it will return false., but it also would not clear out that bad input, and the single body of this while loop to print out please enter a valid integer repeats forever right ?

what about if you loop to
- display a prompt
- try to read any input from the user
- try to see if the input we read is an integer, if so we are done. if not, error, and then continue.

it looks like the way hasNextInt() works is it tests to see if the next thing in the scanner is an int, if it is not it will return false., but it also would not clear out that bad input, and the single body of this while loop to print out please enter a valid integer repeats forever right ?

what about if you loop to
- display a prompt
- try to read any input from the user
- try to see if the input we read is an integer, if so we are done. if not, error, and then continue.

That looks really good and I appreciate the help but I think its a bit to complex for this assignment. I did try and implement that though and came up with some error. I am not familiar with the try and catch ideas of java yet. Anything a bit more simpler to accomplish what I have to do would be good. I am going to keep messing around with this.

11-04-2009, 02:11 AM

Keno777

Still need a bit of help, I have tried a few different things but no luck. Still stuck with the original loop idea that I had but wont work.