1

I'm comparing the input from a TextEdit with a answer from the "answerList". Now I'm wondering: Why does the .equals() not compare the "uinput" String? Could someone explain this to me and put it in use in the code?

Thanks in advance and have a good day!

package ...

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

public TextView view1;
public String uinput;
public EditText edit1;
public TextView score_view;
public int score = 0;

public String[] questionList = {
        "lux, luces",
        "munus, munera",
        "neglere",
};

public String[] answerList = {
        "(dag)licht, dag",
        "taak",
        "verwaarlozen",
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.edit1 = findViewById(R.id.edit1);
    this.view1 = findViewById(R.id.view1);
    this.score_view = findViewById(R.id.score_view);
    this.uinput = edit1.getText().toString();
    view1.setText(questionList[0]);
}

    public void check(View view) {
        if (uinput.equals(answerList[0])) {
            edit1.setBackgroundColor(Color.parseColor("#00FF00"));
            score++;
            score_view.setText(score);
        } else {
            edit1.setBackgroundColor(Color.parseColor("#FF0000"));
        }
}

}

2
  • 2
    Is you question why in the check method the uinput.equals(answerList[0]) might not succeed? If so, it is likely because unless you set the uinput to the updated value of edit1.getText() elsewhere, it only has the value when it was first created, which would be an empty String. Try printing the the value of uinput before the test in the check method. If I have not understood the issue, please elaborate the specific issue you are seeing by providing current results and expected results. Commented Sep 1, 2018 at 16:50
  • Thanks this solved the problem! Commented Sep 1, 2018 at 17:06

2 Answers 2

1

The OP's question concerned a comparison of the uinput to an element in the array questionList. In the check method, the comparison was performed against the uinput, but the value of uinput was not being updated prior to the check.

public void check(View view) {
    // ADD HERE: update the value of the input
    uinput = edit1.getText().toString();

    if (uinput.equals(answerList[0])) {
        edit1.setBackgroundColor(Color.parseColor("#00FF00"));
        score++;
        score_view.setText(score);
    } else {
        edit1.setBackgroundColor(Color.parseColor("#FF0000"));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

remove this keyword

......
edit1 = findViewById(R.id.edit1);
view1 = findViewById(R.id.view1);
score_view = findViewById(R.id.score_view);

......

add this in OnClick() method

uinput = edit1.getText().toString();

7 Comments

Why would removing this make a difference?
because this key word referencing the context of the activity here. which is actually redundant
Hey, thank for the respons. I still get a false return from the method when I remove the this keyword? And how does redundancy makes the method return false?
Redundant or not, how would removing this solve the OP's issue? I don't see how this answer addresses the OP's question.
void Check(View) is his onClick() method. right? show what are you entering in the editText at run time?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.