1

I have been working on a simple dice poker project and I can't seem to get my program to ask the user to replay without repeating a couple line.

Example:

        private static boolean promptForPlayAgain(Scanner inScanner) {
    
        System.out.println("Would you like to play again [Y/N]?: ");
        String str = inScanner.nextLine();
        
        while (!(str.equalsIgnoreCase("Y") || str.equalsIgnoreCase("N"))){
            System.out.println("ERROR! Only 'Y' or 'N' allowed as input!");
            System.out.println("Would you like to play again [Y/N]?: ");
            str = inScanner.nextLine();
        }
        
        if (str.equalsIgnoreCase("Y")){
            return true;
        }
        
        if (str.equalsIgnoreCase("N")){
            return false;
        }
        return false;
        
}

That is my code and before the user can type anything, it outputs:

Would you like to play again [Y/N]?:

ERROR! Only 'Y' or 'N' allowed as input!

Would you like to play again [Y/N]?:

It seems like it skipping over my original str declaration and executing the while loop. What am I doing wrong?

Community
  • 1
  • 1
  • 5
    It works for me. What are you passing to the method? – Paul Boddington Nov 12 '15 at 02:14
  • 4
    What was the previous call to `Scanner` (before the method is invoked)? – Elliott Frisch Nov 12 '15 at 02:25
  • 2
    [Look at this](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods), probably you have the same issue from a code you have not posted here. – Codebender Nov 12 '15 at 02:26
  • Thanks for the speedy replies! I declared a new Scanner in main and passed that through the method and now it works. I am still going to look through my code and find out where the previous call to the original Scanner was in order to patch that up. – Richard Wilde Nov 12 '15 at 02:39
  • 1
    Additional follow up: I found that a Scanner.nextInt in a separate method was causing the problem. Again, thanks for the help! – Richard Wilde Nov 12 '15 at 02:43

0 Answers0