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.
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 ?
Source: Stackoverflow.com