0

I am trying to write a code that compares the users string to the string 'm' or 'f' and repeating the loop if the users answer doesn't match either letter

    public static void main(String[] args) {

    String om = JOptionPane.showInputDialog("I will calculate the number of chocolate bar you should eat to maintain your weigth\n"
            + "Please enter letter 'M' if you are a Male\n"
            + "'F' if you are a Female");
    JOptionPane.showMessageDialog(null, om.equalsIgnoreCase("m"));
// if i don't do this, it says the variables have not been initialized
    boolean m = true;
    boolean f = true;
    if (om == "m"){
     m = om.equalsIgnoreCase("m");
    } else if ( om == "f"){
     f = om.equalsIgnoreCase("f");
    }
    JOptionPane.showMessageDialog(null, "The m is: " + m);
    JOptionPane.showMessageDialog(null, "The f is: " + f);
//if the user enters one variable the other variable becomes false, thus 
 // activating the while loop
    while (m == false || f == false){
        om = JOptionPane.showInputDialog("Please enter letter 'M' if you are a Male\n"
                + "'F' if you are a female");


    }
  /* i tried using '!=' to compare the string that doesn't work, i tired converting the strings to number to make it easier something like the code below:

int g = 2;
if (om = "m"){
g = 0
}
while (g != 0){

}

that doesn't work either

So i tried using boolean which worked but if the user doesn't enter one letter the other letter becomes false and activates the while loop

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Swiss Swagg
  • 11
  • 2
  • 6
  • Strings aren't compared using `==`, but `.equals`, similar to what you did with `equalsIgnoreCase`. – Dave Newton Feb 01 '16 at 21:40
  • i used "variable".equals(om); in the while loop but it didn't solve the problem, anything input kept running the loop – Swiss Swagg Feb 01 '16 at 21:45
  • The code is confusing also. your initial check is not case insensative, but you are setting the variable m or f equal to the check for case insensative. So you prompt the user to enter 'M' or 'F' uppercase, then your if does an ==. your if should be if (om.equalsIgnoreCase("m"). you could also have possible simplification by removing the if and else and just do m = om.equalsIgnoreCase("m"); – Brian S Feb 01 '16 at 21:47

5 Answers5

1

You should compare strings with

string1.equals(string2)

not with

string1 == string2
Sergey Frolov
  • 1,317
  • 1
  • 16
  • 30
0

You have to use the following for strings:

if (om.equalsIgnoreCase("m") {
    ....
}

When you use ==, you are comparing the reference which the values are pointing to whereas .equals() is comparing the actual value.

For example:

String a = "foo";
String b = "foo";

a == b  // This will return false because it's comparing the reference
a.equalsIgnoreCase(b)  // This however will return true because it's comparing the value
Chris G
  • 80
  • 1
  • 2
  • 9
sparkhee93
  • 1,381
  • 3
  • 21
  • 30
0

Don't use == for comparing strings, use equals.

Also the test

m == false || f == false

is not what you want, it always evaluates to true:

  • if you enter 'M' then (f == false) evaluates to true.

  • if you enter 'F' then (m == false) evaluates to true.

  • if you enter 'X' then (m == false) is true and (f == false) is true.

Logical OR means that A OR B is true if either A is true, or if B is true.

It should be (!m && !f) which means "your input is not 'M' and your input is not 'F'".

Or equivalently, write it as !(m || f): "it is not the case that the input is either 'M' or 'F'".

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
0

In addition to changing the operators from == to .equals(), you also need to decide if setting them both to true before you test for validation is necessary. I would set them to false so you don't need to change the values of the boolean m and the boolean f in the validation statements.

    boolean m = true;
    boolean f = true;

    if (om == "m"){
     m = om.equalsIgnoreCase("m");
    } else if (om == "f"){
     f = om.equalsIgnoreCase("f");
    }

could be

    boolean m = false;
    boolean f = false;

    if (om.equalsIgnoreCase("m")){
     m = true; //do not need to address the f because its already false.
    } else if (om.equalsIgnoreCase("f")){
     f = true;
    }

It's quicker and easier to read.

hallmanitor
  • 318
  • 1
  • 4
  • 9
-1

Try setting your boolean variables as False.

boolean m = false; boolean f = false;

This should give you the desired results.

  • They're doing a check already in the `if (om == "m")` section and setting booleans. The issue is the string comparison using `==`. – Tah Feb 01 '16 at 22:09