46

I'm unable to compare two strings using the following code:

I have a string named "gender" which will have "Male" or "Female" as its value.

if(gender == "Male")
   salutation ="Mr.";
if(gender == "Female")
   salutation ="Ms.";

This didn't work, so I tried the following:

String g1="Male";
String g2="Female";
if(gender.equals(g1))
   salutation ="Mr.";
if(gender.equals(g2))
   salutation ="Ms.";

Again, it didn't work. Can someone please tell me how to compare string values using the if statement.

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
Raghav Kumar
  • 489
  • 1
  • 5
  • 6

17 Answers17

97

Try this

if(gender.equals("Male"))
 salutation ="Mr.";
if(gender.equals("Female"))
 salutation ="Ms.";

Also remove ;(semi-colon ) in your if statement

if(gender.equals(g1));

In Java, one of the most common mistakes newcomers meet is using == to compare Strings. You have to remember, == compares the object references, not the content.

Linga
  • 10,379
  • 10
  • 52
  • 104
13

String unlike int or other numeric variables are compared in Java differently than other languages.

To compare Strings in Java (android) it is used the method .compareTo();

so the code should be like this:

if(gender.compareTo("Male")==0){
   salutation ="Mr.";
}
if(gender.compareTo("Female")==0){
   salutation ="Ms.";
}
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
ManuelAlvaradoh
  • 131
  • 1
  • 2
10

In Java we don't compare string as you are doing above... Here is String comparison...

    if (gender.equalsIgnoreCase("Male")) {
        salutation = "Mr.";
    } else if (gender.equalsIgnoreCase("Female")) {
        salutation = "Ms.";
    }
SilentKiller
  • 6,944
  • 6
  • 40
  • 75
8

I think the above mentioned answer is correct.Because == is for testing whether two strings are the same object,whereas .equals() tests whether two strings have the same value.

anjaly
  • 518
  • 5
  • 12
4

The == operator checks to see if two objects are exactly the same object. Two strings may be different objects, but have the same value (have exactly the same characters in them). Use the .equals() method to compare strings for equality.

http://www.leepoint.net/notes-java/data/strings/12stringcomparison.html

bema
  • 385
  • 2
  • 8
4

Your gender == "Male" is actually comparing the object references for the object gender and a different object Male. What you have to use is the .equals() method to compare the value of the objects.

midhunhk
  • 5,560
  • 7
  • 52
  • 83
3

try this

String g1="Male";
    String g2="Female";
    if(gender.equals(g1))
       salutation ="Mr.";
    if(gender.equals(g2))
       salutation ="Ms.";

you were ending your if statement if(gender.equals(g1)); <<--- here by adding ";"

Shiv
  • 4,569
  • 4
  • 25
  • 39
3
String g1="Male";
String g2="Female";
String salutation="";
String gender="Male";
if(gender.toLowerCase().trim().equals(g1.toLowerCase().trim()));
   salutation ="Mr.";
if(gender.toLowerCase().trim().equals(g2.toLowerCase().trim()));
   salutation ="Ms.";
Arun C
  • 9,035
  • 2
  • 28
  • 42
3

try this.

        String g1 = "Male";
        String g2 = "Female";
        String gender = "Male";
        String salutation = "";
        if (gender.equalsIgnoreCase(g1))

            salutation = "Mr.";
        else if (gender.equalsIgnoreCase(g2))

            salutation = "Ms.";
        System.out.println("Welcome " + salutation);

Output:

Welcome Mr.
Imran Ali
  • 539
  • 4
  • 17
2

This should work:

if(gender.equals("Male")){
 salutation ="Mr.";
}
else if(gender.equals("Female")){
 salutation ="Ms.";
}

Remember, not to use ; after if statement.

Jainendra
  • 24,713
  • 30
  • 122
  • 169
2

In addition if you want to compare if two strings are different, you can use :

String mystring = "something";
!mystring.equals("whatever")

It will return true!

HMD
  • 2,202
  • 6
  • 24
  • 37
0
if(gender.equals(g1)); <---
if(gender == "Female"); <---

You have semicolon after if.REMOVE IT.

Vipul
  • 27,808
  • 7
  • 60
  • 75
0

Actually every code runs well here, but your probleme probably come from your gender variable. Did you try to do a simple System.out.println(gender); before the comparaison ?

e1che
  • 1,241
  • 1
  • 17
  • 34
0

You can use, contentEquals() function. It may help you..

Ashwin Mothilal
  • 2,462
  • 1
  • 16
  • 21
0

This one work for me:

 if (email.equals("fahim@gmail.com") && pass.equals("123ss") ){
        Toast.makeText(this,"Logged in",Toast.LENGTH_LONG).show();
    }
    else{

        Toast.makeText(this,"Logged out",Toast.LENGTH_LONG).show();
    }
0

Actually we could help better if we could see what is the input coming gender

However you can try following, maybe it's a case issue, like the incoming gender is male instead of Male

if(gender.equalsIgnoreCase("male"){
    salutation = "Mr.";
} else if(gender.equalsIgnoreCase("female"){
    salutation = "Ms.";
}

Or if this didn't worked, try getting the gender and let us know what it is, so we can help better. You can print gender like this

System.out.println(gender);

You may see the output on the Console on your IDE

DiLDoST
  • 335
  • 3
  • 12
-1

Try it:

if (Objects.equals(gender, "Male")) {
    salutation ="Mr.";
} else if (Objects.equals(gender, "Female")) {
    salutation ="Ms.";
}
bummi
  • 27,123
  • 14
  • 62
  • 101
Quang Dai Ngo
  • 41
  • 1
  • 5