-1

I make android questions app. I can not find the right answer. Answer list.

Right answer: String answer = quizList.get(position).getAnswer();

Button Click:

    buttonA.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



            if (answer =="A"){

                lblsignboard.setText("Ok");

            }
            else
            {

                lblsignboard.setText("No");


            }
        }
    });

Answer = "A", but this code does not work. Always gives the answer is no.

Deniz
  • 115
  • 1
  • 2
  • 11

5 Answers5

3

When you are comparing strings in java,always use .equals

Because the function (.equals) checks the actual contents of the string, the == operator checks whether the references to the objects are equal.

and now furthur using equalsignorecase you need not to worry about search string is in caps or small.

buttonA.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (equalsIgnoreCase("a")){
                lblsignboard.setText("Ok");
            }else{
                lblsignboard.setText("No");
            }
        }
    });
Vivek
  • 189
  • 1
  • 7
2

You can use this way too, to keep you code working even the answer key is of different case. Use either equals("A") or equalsIgnoreCase("a").

 buttonA.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (answer.equals("A") || equalsIgnoreCase("a")){
                lblsignboard.setText("Ok");
            }else{
                lblsignboard.setText("No");
            }
        }
    });
iSandeep
  • 597
  • 8
  • 24
1

In Android we make use of two things to compare strings: 1. equals : this is used when you want to compare even case of the string. 2. equalsIgnoreCase: This is just for content comparison. Here is the small illustration as how it works:

   String myString = "heLLo";

    if(myString.equals("hello"))
    {
     System.out.print("equals");
    }
    else if(myString.equalsIgnoreCase("hello"))
    {
        System.out.print("equalsIgnoreCase"); 
    }

Here equalsIgnoreCase will be printed.

1

Strings can only compare by .equals method not by ==. Also you have to check string is null or not because .equals method may invoke nullpointerexception and you app may crash.

    buttonA.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      if(answer!=null &&(answer.equals("A")||equalsIgnoreCase("a"))) {
            lblsignboard.setText("Ok");
      } else {
            lblsignboard.setText("No");
      }
    }
});
pooja majoka
  • 136
  • 4
0

String can compare is .equals or .eqalignorecase not a '==' sign.'==' using a int value compare.

String answer=item.get(position).getAnswer();
    if(answer!=null && (answer.equals("A")||equalsIgnoreCase("a")){
            Log.e("Comapare","----------Success"); 
    }else{
        Log.e("Comapare","----------not compare");
    }
Pradip
  • 57
  • 1
  • 3