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?