Sunday, July 8, 2018

java - String array value not matching edittext











I have a string array with values which I am trying to compare against text inputted into an edittext field. However despite both the edittext and the value in the array being the same my if (answer == guess) never resolves to true.



I've put debugging via Toast messages and it shows both answer and guess are the same. Can someone help with this please? Code below:



    final EditText et;

et = (EditText) findViewById(R.id.editText1);
String guess = et.getText().toString();

String answer = LinesFromFile[LineNumber];

if (answer == guess)
{
Toast msg = Toast.makeText(getBaseContext(), answer + " " + guess + " right", Toast.LENGTH_LONG);
msg.show();
}

else
{

Toast msg = Toast.makeText(getBaseContext(), answer + " " + guess + " wrong", Toast.LENGTH_LONG);
msg.show();
}

Answer



You can't do string comparisons using == ... you need to use String.equals()



if (answer.equals(guess))
{
...


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...