0

I'm not so good at programming and can't tell why this isn't working. Whatever I input, it always go straight to the else statement.

    public void pizzaIntro()
    {
    Scanner user_input = new Scanner(System.in);
    String user_command = "null";
    String apology = "I'm sorry it appears there has been some kind of mistake in your order.";
    System.out.println("Welcome to " + cM + " here we strive to deliver excellent services to all our customers!");
    System.out.println("The current prize for pizza is $" + bP + " and an extra $" + tP + " per topping.");
    System.out.println(); System.out.println();
    while(user_command != "exit")
    {
        System.out.print("Would you like toppings?(yes/no):");
        user_command = user_input.next();
        if(user_command.toLowerCase() == "yes")
        {
            System.out.println("Good Eat Your Pizza.");
        }
        else if (user_command.toLowerCase() == "no")
        {
            System.out.println("Well Ok Then!");
        }
        else
        {
            System.out.println(apology);
            System.exit(1);
        }
    }
    pic1.show();
}
Jeevan Patil
  • 6,029
  • 3
  • 33
  • 50

4 Answers4

2

Use equals method to compare strings. To know the difference between == and equals, read https://stackoverflow.com/questions/7311451/difference-between-equals-and-instanceof You will get a clear idea of what to use when.

while(!(user_command.equals("exit")) {
    System.out.print("Would you like toppings?(yes/no):");
    user_command = user_input.next();
    if(user_command.toLowerCase().equals("yes"))
    {
        System.out.println("Good Eat Your Pizza.");
    }
    else if (user_command.toLowerCase().equals("no"))
    {
        System.out.println("Well Ok Then!");
    }
    else
    {
        System.out.println(apology);
        System.exit(1);
    }
}
Community
  • 1
  • 1
Jeevan Patil
  • 6,029
  • 3
  • 33
  • 50
1

Use equals method instead of ==

user_command.toLowerCase().equals("yes")

In Java, == always just compares two references. Its OK to use it for primitive data types. String is not primitive datatype. String is a object. You should use equals method.

In your case you can consider using equalsIgnoreCase method to ignore the Case.

Jayamohan
  • 12,734
  • 2
  • 27
  • 41
1

Use equalsIgnoreCase. It is more safe

public void pizzaIntro()
    {
        Scanner user_input = new Scanner(System.in);
        String user_command = "null";
        String apology = "I'm sorry it appears there has been some kind of mistake in your order.";
        System.out.println("Welcome to " + cM
                           + " here we strive to deliver excellent services to all our customers!");
        System.out.println("The current prize for pizza is $" + bP
                           + " and an extra $" + tP + " per topping.");
        System.out.println();
        System.out.println();
        while (!user_command.equalsIgnoreCase("exit"))
        {
            System.out.print("Would you like toppings?(yes/no):");
            user_command = user_input.next();
            if (user_command.equalsIgnoreCase("yes"))
            {
                System.out.println("Good Eat Your Pizza.");
            }
            else if (user_command.equalsIgnoreCase("no"))
            {
                System.out.println("Well Ok Then!");
            }
            else
            {
                System.out.println(apology);
                System.exit(1);
            }
        }
        pic1.show();
    }
Fathah Rehman P
  • 8,401
  • 4
  • 40
  • 42
1

Never compare any Java objects with "==" (only primitives like int, long, double, etc.). It should read:

if(user_command.toLowerCase().equals("yes")) {
  ...
}

otherwise you check if the location of the object is the same, not the content.

In this particular example you might just want to use String.equalsIgnoreCase(...) instead.

Peter Becker
  • 8,795
  • 7
  • 41
  • 64