String comparison - Android [duplicate]

Solution 1:

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.

Solution 2:

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