• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps if() statement not being entered

Slaine82

Newbie
Hi All,

I am having a problem which is driving me crazy :confused:, I am sure it is something simple but I just can't see what it wrong.

Basically I am having a problem with an if statement not being entered after using a equals statement even though both side match exactly, the code I have is

String currentQCorAns = correctAnswers[currentQNo];
//this pulls in an answer from an array in the resources value is "Hill Of Tara"
String currentSelectedString = (String)(selectedRadioButton.getText());
//this gets the current selected radio buttons text value is "Hill Of Tara"

if(currentQCorAns == currentSelectedString ){
Toast.makeText(mContext, "Question is correct", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, "Question is incorrect", Toast.LENGTH_SHORT).show();
}

I have tried everything from casting the variables to the same type to be sure. Even the following doesn't work:-

if(currentQCorAns == "Hill Of Tara")

It always enters the else part of the block. Can anyone help? Is it something to do with the fact I am pulling in the values from a resources file.

Thanks!
 
You should not use "==" when comparing two Strings. You should use the equals() method.

if (currentQCorAns.equals(currentSelectedString)) {...}

The == operator compares the reference variables, which are different.
 
in general , do not compare objects with the == , use .equals()
also .. when posting a code .. use the
Code:
 tag
 
Back
Top Bottom