0

I am a beginner in android studio. I am working on a quiz app . The app should check for two strings to be compared and give the correct answer. But comparing the two strings (even though if they are same) is not giving the correct output. Instead it is directly going to the final return statement in the code. Here’s the code:

// ...
EditText Answer1 = (EditText) findViewById(R.id.answer1);
String ans = Answer1.getText().toString();
Log.v("MainActivity", "City name :" + ans);
String answer= String.valueOf(Answer1);
// ...

public String YourAnswers(String ans, boolean isDT, boolean isHC, boolean isBO,String answer) {

    String Message = "1.:You answered  \n"+ans+  "\n"    +ques1(answer);
    Message = Message + "  \n 2.:  \n" +question2(isDT,isHC,isBO) ;
    return Message;
}

public String ques1(String answer) {
    if (answer == "Jefferson City"){//||ans=="Jeff City"||ans=="Jeffcity"||ans=="Jeffersoncity"){
        return "correct";
    }
    else if(answer =="Jeff City") {
        return "correct.";
    }
    else if(answer =="Jeffcity"){
        return "correct..";
    }
    else if(answer.equals("Jeffersoncity")){
        return "correct.....";
    }

    return "Sorry,but the correct ans is Jefferson City";
}

When it enters into ques1(), it is directly going to the last statement i.e. return "Sorry,but the correct ans is Jefferson City";. When I enter the correct answer too it is returning the above mentioned line.

Any ideas as to why this might be happening?

D. Ataro
  • 1,711
  • 17
  • 38
SriVVV
  • 29
  • 2

4 Answers4

5

"==" is not the correct way to compare contents of a String in java.

use string1.equals(string2)

voldy
  • 359
  • 1
  • 8
  • 21
1

Use .equals() throughout instead of ==.

However try this code to allow for any capitalization of answer using toLowerCase():

import java.util.Arrays;

...
...

String[] answers = {"jefferson city","jeff city", "jeffcity", "jeffersoncity"};
if(Arrays.asList(answers).contains(answer.toLowerCase())) {
  return "Correct";
} else {
  return "Sorry,but the correct ans is Jefferson City";
}
Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
1

Background

There are two common methods used in order to compare two strings in Java. In your example, rather than making use of one of them, you’re using == which is an equality operator—these, you can’t use to check for equality on Strings.

The first method you may use is equals, located on the String. This will check and see if all characters contained by the String are exactly the same—that is, it also makes sure the String you’re giving it has the same letter case as the one you’re comparing it to.

Here’s an example:

"Hello, world!".equals("Hello, world!"); // ‘equals’ returns true
"Hello, world!".equalsIgnoreCase("hello, world!"); // ‘equals’ returns false

The second method you may use is equalsIgnoreCase, also located on the String. This will return true if the Strings compared are the same, but don’t give into account if they’re of different cases—it doesn’t care.

Let’s repeat the previous example and see what happens:

"Hello, world!".equals("Hello, world!"); // ‘equals’ returns true
"Hello, world!".equalsIgnoreCase("hello, world!"); // ‘equals’ returns true

See how both returned true this time around?

Solution

With all this in mind, we can go ahead and fix your code up:

public String ques1(String answer)
{
    if (answer.equals("Jefferson City"))
    {
        return "correct";
    }
    else if(answer.equals("Jeff City"))
    {
        return "correct.";
    }
    else if(answer.equals("Jeffcity"))
    {
        return "correct..";
    }
    else if(answer.equals("Jeffersoncity"))
    {
        return "correct.....";
    }

    return "Sorry,but the correct ans is Jefferson City";
}

That’s going to work just great!

Moreover

That I put { on new lines doesn’t affect the outcome of your program. There are however good reasons as to why you’d want to put them on separate lines. The reason I’m writing this is because I’m highly fond of bringing up the debate all over again—making people mad.

Community
  • 1
  • 1
D. Ataro
  • 1,711
  • 17
  • 38
0

Use .equalsIgnoreCase on Strings if not case sensitive. Also to avoid nullpointer exception use the following syntax

"Jeff City".equals(stringVar);

Ronny Shibley
  • 2,030
  • 22
  • 25