Reputation: 693
I am having this strange behavior when performing a method which the if statement where both values are true but it fails to go over the if statement.
Here is the code and how it looks like. On top right where it is circled red is the value of className.
If anyone has an idea why this is occurring or might have a better way of doing it please respond or guide me to a better coding logic to that I am already using.
On debug className = "rwb"
public void ClassReturn(){
String tempName = getIntent().getExtras().getString("CLASS_NAME");
if(tempName == null){
Log.i("Intent Delivery", "Intent deliver has failed.");
}else{
String className = tempName; // This is return back to the correct class your in
if(className == "rwb"){
Intent intent = new Intent(BasicOption.this, ReadWholeBook.class);
startActivity(intent);
}
}
}
Upvotes: 0
Views: 189
Reputation: 109
use the equals method to validate Strings, but also you have to be sure about the content, so my recomendation is use this :
myString.trim().equals(myString2.trim());
you could turn to upper case the strings too.
Saludos
Upvotes: 1
Reputation: 15729
Squinting at the code, you are comparing a String using ==
if (className == "rub")...
You should use equals() instead, i.e. (I put "rub" first cause I know that will never be null)
if ("rub".equals(className))...
Upvotes: 1
Reputation: 9189
In Java you cannot overload operators therefore what you're doing is comparing the object ID instead of comparing the String's logical value. Switch to string.equals(other_string) and it will work as you expect.
Upvotes: 2
Reputation: 8251
Use String.equals()
in this situation. ==
mainly is for checking if the object is the same exact object.
Upvotes: 2
Reputation: 66637
Don't use == comparison on Strings, use className.equals("anotherString");
Upvotes: 2
Reputation: 405745
You're using ==
to compare two Strings. Use the equals
method instead.
Upvotes: 2
Reputation: 5546
Use .equals() to check if two objects are logically equal, use == the check if they are the same object.
Strings in java can be more complicated because of interning but basically you want to use the .equals() or .equalsIgnoreCase() to check the contents of the two strings are the same.
Upvotes: 14