-1

I am building a quiz app where I take two text input from the user and compare it using if else statement, but the code is skipping the part where "if and if" true part and only executing the else if part.

 public void checkAnswer_condition_q2(View view) {


    EditText blank_one = (EditText)findViewById(R.id.fill_condition1);
    EditText blank_two = (EditText)findViewById(R.id.fill_condition2);

    if(btn_continue.getText() == "Continue"){
        Intent intent = new Intent(condition_q2.this, loops.class);
        startActivity(intent);
        this.finish();
    }

    if(blank_one.getText().toString() == "if" && blank_two.getText().toString() =="else"){
        Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show();
        btn_continue.setText("Continue");

    }
    else if(blank_one.getText().toString() != "if" ||  blank_two.getText().toString() !="else"){
        Toast.makeText(this, "Try Again", Toast.LENGTH_SHORT).show();
    }
}
Ved Sarkar
  • 241
  • 1
  • 8
  • 24
  • 3
    that's not `if else statement not working`, it's just incorrect way to compare strings. Search for `java compare strings` – Vladyslav Matviienko May 01 '18 at 19:22
  • 3
    Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – S.L. Barth May 01 '18 at 19:23

1 Answers1

1

Use equals to compare Strings contents not == , also you've forgotten to call toString() in the btn_continue:

if(btn_continue.getText().toString().equals("Continue")){
        Intent intent = new Intent(condition_q2.this, loops.class);
        startActivity(intent);
        this.finish();
    }

    if(blank_one.getText().toString().equals("if") && blank_two.getText().toString().equals("else")){
        Toast.makeText(this, "Correct", Toast.LENGTH_SHORT).show();
        btn_continue.setText("Continue");

    }
    else if(!blank_one.getText().toString().equals("if") ||  !blank_two.getText().toString().equals("else")){
        Toast.makeText(this, "Try Again", Toast.LENGTH_SHORT).show();
    }
Levi Moreira
  • 11,587
  • 4
  • 30
  • 45