47

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.

2
  • 1
    Have you get solution from answers ? Commented Apr 22, 2013 at 9:30
  • 1
    If that is not working, it is because gender is something else. Add this to figure it what is Log.d("GENDER", gender) Commented Mar 10, 2021 at 22:49

17 Answers 17

98

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.

Sign up to request clarification or add additional context in comments.

7 Comments

If you explain why, I'll +1 this :)
Look at your code carefully and remove the ; at the end of both the if statements.
Sorry, i wasn't using ; after the if stmt in my actual code, error while typing the question here!
@RaghavKumar What's the input?
For null-safety, I always put the constant on the left side: if("Male".equals(gender))
|
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."; } 

Comments

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."; } 

Comments

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.

2 Comments

This is a better explanation why not to use ==
there is no "above" answer, for the order varies with number of votes. please edit and specify, if possible. tks
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

Comments

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.

Comments

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 ";"

Comments

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."; 

Comments

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. 

Comments

3

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!

Comments

2

This should work:

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

Remember, not to use ; after if statement.

Comments

0
if(gender.equals(g1)); <--- if(gender == "Female"); <--- 

You have semicolon after if.REMOVE IT.

Comments

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 ?

Comments

0

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

Comments

0

This one work for me:

 if (email.equals("[email protected]") && pass.equals("123ss") ){ Toast.makeText(this,"Logged in",Toast.LENGTH_LONG).show(); } else{ Toast.makeText(this,"Logged out",Toast.LENGTH_LONG).show(); } 

Comments

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

Comments

-1

Try it:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.