For some reason I am getting different results from an input of lower case “y” than an input of upper case “Y”. An input of “y” executes the code in the if statement but an input of “Y” does not. Also, an input of “Y” does not execute the break; after else. Why is this and what am I doing wrong?
Scanner playAgain = new Scanner (System.in);
System.out.println ("Play Again (Y/N)");
if ((playAgain.next().equals("y"))||(playAgain.next().equals("Y")))
{
Game theGame1 = new Game(0);
currentPts = currentPts + theGame1.play(total);
System.out.println("Current total is:" + " " + currentPts);
}
else break;
In this condition:
You’re calling
playAgain.next()twice – so it’s fetching two different strings from the user.I’m sure you only intended to fetch one string:
Or alternatively, just use
equalsIgnoreCase, which means you can just read once: