I am a beginner in android studio. I am working on a quiz app . The app should check for two strings to be compared and give the correct answer. But comparing the two strings (even though if they are same) is not giving the correct output. Instead it is directly going to the final return statement in the code. Here’s the code:
// ...
EditText Answer1 = (EditText) findViewById(R.id.answer1);
String ans = Answer1.getText().toString();
Log.v("MainActivity", "City name :" + ans);
String answer= String.valueOf(Answer1);
// ...
public String YourAnswers(String ans, boolean isDT, boolean isHC, boolean isBO,String answer) {
String Message = "1.:You answered \n"+ans+ "\n" +ques1(answer);
Message = Message + " \n 2.: \n" +question2(isDT,isHC,isBO) ;
return Message;
}
public String ques1(String answer) {
if (answer == "Jefferson City"){//||ans=="Jeff City"||ans=="Jeffcity"||ans=="Jeffersoncity"){
return "correct";
}
else if(answer =="Jeff City") {
return "correct.";
}
else if(answer =="Jeffcity"){
return "correct..";
}
else if(answer.equals("Jeffersoncity")){
return "correct.....";
}
return "Sorry,but the correct ans is Jefferson City";
}
When it enters into ques1()
, it is directly going to the last statement i.e. return "Sorry,but the correct ans is Jefferson City";
. When I enter the correct answer too it is returning the above mentioned line.
Any ideas as to why this might be happening?