[java] String comparison - Android

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.

This question is related to java android string

The answer is


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

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

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();
    }

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

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


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


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.


Try it:

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

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!


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


This should work:

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

Remember, not to use ; after if statement.


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

You have semicolon after if.REMOVE IT.


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.

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.


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.


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 ?


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript