-4

I'm creating a quiz activity in which when the user inputs his or her answer into the EditText and presses 'Next' button, it will generate the next random word and also check if you are correct or wrong.

However, for some reason, even though my code was kind of able to detect both the EditText and the correct word answer, it returns back to me the 'wrong answer' code. Can anyone help me please? Thanks! (I'm still new at this...)

*Edited: This is not a duplicate question people. I seriously need someone to look at my code specifically and tell me exactly what is wrong because I don't know. It's not a general question which can be solved (for me anyway) just by looking at other people's questions because I'm still very new at this. Thanks a lot.*

btnNext.setOnClickListener(new OnClickListener(){


        @Override
        public void onClick(View v) {
            EditText etGuess= (EditText) findViewById(R.id.editTextGuess); 
            etguess2 = etGuess.getText().toString(); 
            //these two above were actually placed outside but no luck. didnt work here either

            String etg2str = etguess2; // the users edittext input
            String tgstr = theguess; // the correct answer, the guess is from outside

            if (myCD > 0 && etg2str.toString() == tgstr.toString()){

            myCD--; //this is a countdown of the number of qns
            Toast.makeText(getApplicationContext(), "correct answer"+ myCD + rantitle + " now",
                       Toast.LENGTH_LONG).show(); // this has nvr appeared for me
            savePreferences("countDown", myCD);
            RandomTitle(); //calling for random word on next load
            Intent iNext = new Intent(QuizActivity.this, 
                    QuizActivity.class);
            iNext.putExtra("dataR", rantitle); //inputting random word for next load
            startActivityForResult(iNext, 1);
            finish();
            }

            else if (myCD > 0 &&  etg2str.toString() != tgstr.toString()){

                myCD--;
                Toast.makeText(getApplicationContext(), "wrong answer " + myCD + rantitle + etguess2 + theguess + " now",
                           Toast.LENGTH_LONG).show();
            //now this one above appears for me everytime yet even tho etguess2 and theguess appears the same, I get wrong number


                savePreferences("countDown", myCD);
                RandomTitle();
                Intent iNext = new Intent(QuizActivity.this, 
                        QuizActivity.class);
                iNext.putExtra("dataR", rantitle);
                startActivityForResult(iNext, 1);
                finish();

            }

            else{

                Intent iScore = new Intent(QuizActivity.this, 
                        ScoreActivity.class);
                startActivityForResult(iScore, 1);
                myCD = 10;
                savePreferences("countDown", myCD);
            finish();

            }
        }


        });         
    }
Fuchsia
  • 77
  • 1
  • 7

1 Answers1

3

you need to use .equals() to compare strings:

etg2str.toString().equals(tgstr.toString());

the == operator is for comparing primitive types like for example int

Accordingly for your not equals case you can use ! operator:

!etg2str.toString().equals(tgstr.toString());
donfuxx
  • 11,277
  • 6
  • 44
  • 76