-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
  • 251
  • 2
  • 8
  • 25

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,917
  • 4
  • 32
  • 46