0

Here's a snippet of my code for an assignment. I can't seem to get the boolean flag to work properly. When i try to figure it out, either every name gets the discount or no name gets the discount. To clarify the name Mike or Diana should give a discount.

String firstName;     //user's first name  
boolean discount = false;  //flag, true if user is eligible for discount 
int inches;       //size of the pizza
char crustType;     //code for type of crust
String crust; //name of crust
double cost = 12.99;    //cost of the pizza
final double TAX_RATE = .08; //sales tax rate
double tax;       //amount of tax
char choice;      //user's choice
String input;      //user input
String toppings = "Cheese "; //list of toppings 
int numberOfToppings = 0;  //number of toppings

//prompt user and get first name
System.out.println("Welcome to Mike and Diane's Pizza");
System.out.print("Enter your first name:  ");
firstName = keyboard.nextLine();

if (firstName == "mike" || firstName == "diana" || firstName == "Mike" || firstName == "Diana" ||
firstName == "MIKE" || firstName == "DIANA")
{
discount = true;
}

if (discount = true)
{
cost  -= 2.0;
System.out.println ("You are eligible for a $2 discount.");
  • What is going wrong? – PeterK Oct 10 '14 at 22:24
  • In addition to string comparaisons, you should replace `if (discount = true)` with `if (discount == true)`. The first is assigning a value to variable `discount`, the second is comparaison. – AntonH Oct 10 '14 at 22:26
  • @SotiriosDelimanolis string comparison is not the only issue here – guido Oct 10 '14 at 22:27
  • Not a direct answer but that expression would look neater if the firstname variables were both converted to uppercase before comparison. That would only require one comparison per name. Even better, use an in array search. – Gavin Oct 10 '14 at 22:27
  • @guido Ok, I've reopened. – Sotirios Delimanolis Oct 10 '14 at 22:27

2 Answers2

3

First of all, in order to compare Strings, you don't use ==. You need to use the String#equals() method as per this SO question.

if (firstName == "mike" || firstName == "diana" || firstName == "Mike" || firstName == "Diana" || firstName == "MIKE" || firstName == "DIANA")

would be replaced by

if (firstName.equals("mike") || firstName.equals("diana") || firstName.equals("Mike") || firstName.equals("Diana") || firstName.equals("MIKE") || firstName.equals("DIANA"))

However, as Gavin says in the comments to your original question, it would be better to convert entire string to either uppercase or lowercase, to make less comparaisons. Or, as per Pshemo's comment, use equalsIgnoreCase().

You also need to change:

if (discount = true)

which is assigning the value of true to the variable discount, to

if (discount == true)

or, as per Pshemo's comment,

if (discount)
Community
  • 1
  • 1
AntonH
  • 6,359
  • 2
  • 30
  • 40
  • 4
    I would suggest avoiding `if (discount == true)` and instead using `if (discount)`. This way we are sure we will not make mistake like the one form OP code. – Pshemo Oct 10 '14 at 22:37
  • 3
    Also `equalsIgnoreCase` seems like nicer option than manually converting strings to upper-case. – Pshemo Oct 10 '14 at 22:38
  • @Pshemo Both valid points. I edited my answer and credited the edits to you. – AntonH Oct 10 '14 at 23:08
0

I've checked your code with my eclipse. Using

if(firstName == "mike" || firstName == "diana" || firstName == "Mike" || firstName == "Diana" ||firstName == "MIKE" || firstName == "DIANA")

is the problem, because of problems when comparing strings like this. You must have to change it to

if (firstName.equals("mike")){
    discount = true;
}

Then keep in mind that discount is a boolean variable. So you dont have to compare as whether it's true or not.

if (discount) //if will check whether it's true or not if provided with a condition,discount is boolean, so need of comparison

will be very simple for boolean operator.

user562
  • 88
  • 1
  • 11