1

So basically I am creating a quiz for my Android app. However I have noticed that when the user types in the correct answer which is "angry" and has a space next to it the answer appears as wrong. I am wondering how I could change this so that white space after the answer does not get included? Thanks!

public void question3(View view) {

        // Get the typed answer by the user and convert it to lower case.
        EditText question3 = (EditText) findViewById(R.id.question3_typed); 

        String question3_answer = question3.getText().toString().toLowerCase();

        if (question3_answer.equals("angry")) {
            answer3 = "correct";
        } else answer3 = "wrong";
David G
  • 325
  • 1
  • 3
  • 15

1 Answers1

0

You should use trim method of String class which removes any white space in the beginning or end of the string. Modify your code like this

public void question3(View view) {

    // Get the typed answer by the user and convert it to lower case.
    EditText question3 = (EditText) findViewById(R.id.question3_typed); 

    String question3_answer = question3.getText().toString().trim().toLowerCase();

    if (question3_answer.equals("angry")) {
        answer3 = "correct";
    } else answer3 = "wrong";
Suhaib Roomy
  • 2,501
  • 1
  • 16
  • 22