Re: Problem with Scanner ?
Scanner#nextInt doesn't consume the newLine, which remains in the scanner's buffer and is consumed when you next call Scanner#nextLine. You need a "dummy" nextLine to clear the buffer.
Code :
userGuess = myScanner.nextInt();
myScanner.nextLine();
db
Re: Problem with Scanner ?
Thank you.. exactly what I needed to know.
Re: Problem with Scanner ?
ok i have done as you suggested and soaked up the newline after the int is entered. This worked, the program now waits for the user to answer the "play again" question. However, now if the user answers "yes" it repeats the "play again" question over and over until the user enters something else and terminates the program.
I don't think there is an issue with my looping, is there still something I am not understanding about my Scanner usage?
Re: Problem with Scanner ?
Well, considering that you're generating the random number inside the loop, it changes every time the loop repeats. Are you sure that's what you wanted?
db
Re: Problem with Scanner ?
It's because of this
Code :
while((userGuess < 1) || (userGuess > 10))
Your users guess is always going to be between 1 and 10 on the second loop through since you didn't reset it move the userGuess = 0 to inside the first loop to fix this.
Also I would change your second if to an else if to speed up the programme by a millisecond as it doesn't need to check it if it is outside the range.
EDIT:
Changing it to a do while would probably work aswell.
Re: Problem with Scanner ?
Thanks everyone. I needed to change the initialization to happen inside the do-while loop and everything is fine now.
I shall play with your other suggestions as well.