0
        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.
Sam
  • 454
  • 4
  • 18

2 Answers2

2

use while ("y".equals(cont)) instead of while (cont == "y")

To compare String objects in java use .equals() method instead of "==" operator

if you want ignore case use like below

while ("y".equalsIgnoreCase(cont))
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • Hi, I will use this method in the future, however after implementing it into my code the same outcome and problem occurs. – Sam Dec 02 '13 at 20:16
  • @user2964689 update your question with error or exception – Prabhakaran Ramaswamy Dec 02 '13 at 20:17
  • YES, this worked! Thankyou, can you tell me why using nextLine() didn't work but next() did, the difference between the two? – Sam Dec 02 '13 at 20:24
  • 1
    You are getting different data types. You should always use `.nextLine()` then parse the line into what you need. You were getting screwed over by using `.nextXXXX()` followed by `.nextLine()` I think. Try using `.nextLine()` only. I know you have it working but it's the best solution really. If you do that you'll have to parse the money to an int but it will be safer that way. – Ben Kane Dec 02 '13 at 20:38
  • Your `.nextLine()` ate up the return after the 34 typed for `.nextInt()` since `.nextInt()` grabbed 34 but not the line break. – Ben Kane Dec 02 '13 at 20:40
  • You could also just call `input.nextLine()` after `money = input.nextInt();` to eat up the return, then use `cont = input.nextLine();` but that feels kind of shady. – Ben Kane Dec 02 '13 at 20:44
0

As ZouZou says in the comments == compares where two objects are pointing to in memory. equals() compares the value of the two objects so you want to do while(cont.equals('y')

The == operator should really only be used when you are checking to see if two variables point to the same object in your computers memory. In most cases you are better off using equals()

ford prefect
  • 7,096
  • 11
  • 56
  • 83