cont = "y";
while (cont == "y")
{
System.out.println("Enter money.");
money = input.nextInt();
TestMachine.insertMoney(money);
System.out.println("Enter more money? ('y' for yes, anything else for no.)");
cont = input.nextLine();
}
System.out.println("Enter how many tickets do you want.");
When I enter my code, at this part, this is what happens.
Enter money.
34
Enter more money? ('y' for yes, anything else for no.)
Enter how many tickets do you want.
The code will break the while loop before It's recieved input from the user and even though the variable 'cont' appears to still be "y". Could someone help me by explaining why this happens? Thanks.
I've been told to use .equals() instead of == in my code. It now looks like this.
cont = "y";
while ("y".equals(cont))
{
System.out.println("Enter money.");
money = input.nextInt();
TestMachine.insertMoney(money);
System.out.println("Enter more money? ('y' for yes, anything else for no.)");
cont = input.nextLine();
}
System.out.println("Enter how many tickets you want.");
however when I run the code the same problem arises. It breaks the loop before waiting for an input from the user.
Enter money.
34
Enter more money? ('y' for yes, anything else for no.)
Enter how many tickets do you want.