0

i'm trying to create a multiple choice quiz using 4 buttons with array. i couldn't get the variable from the button text

my button text: buttonA: Obama, buttonB: Lincoln, buttonC: Washington, buttonD: Bush the correct answer is Obama (buttonA)

when i change the code on the answer line into answer = "Obama"; (comment line), it works. But when i change into a gettext() from the buttonA text, it didn't works.

i checked the answer variable from gettext(), it returns ""

Please advise, thank you

here's the code

package com.trivia;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MenuWho extends Activity {
    private String keyanswer ,answer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layoutwho);


        Button buttons[] = new Button[4];
        buttons[0] = (Button)findViewById(R.id.buttonA);
        buttons[1] = (Button)findViewById(R.id.buttonB);
        buttons[2] = (Button)findViewById(R.id.buttonC);
        buttons[3] = (Button)findViewById(R.id.buttonD);

        keyanswer = "Obama";
        answer = "";
    }

    public void cekanswer (View view) {

        Button selectedbutton = (Button) view;
           switch (selectedbutton.getId())
           {
              case R.id.buttonA:
                  //answer = "Obama";
                  answer = selectedbutton.getText().toString();
                  break;

              case R.id.buttonB:
                  answer = (String) selectedbutton.getText();
                  break;

              case R.id.buttonC:
                  answer = (String) selectedbutton.getText();
                  break;

              case R.id.buttonD:
                  answer = (String) selectedbutton.getText();
                  break;
           }

           //cek answer
        if (answer == keyanswer)
                {
            Toast.makeText(this,"Correct !", Toast.LENGTH_LONG).show();
                }
            else {
                Toast.makeText(this,"Wrong !", Toast.LENGTH_LONG).show();
                }
}

}
manlio
  • 18,345
  • 14
  • 76
  • 126
vincmeister
  • 181
  • 4
  • 4
  • 14

3 Answers3

1

User answer.equals(keyanswer). Equals checks the data of the strings, == only checks if the references are the same.

Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
1

With Strings in Java, using == only looks to see if the references are the same. You must use the method .equals() to see if the actual data of the strings is the same.

Here is another SO post on the subject .equals() vs ==

And the Java documentation..

String#equals

So for your answer specifically, change the line if (answer == keyanswer) to if (answer.equals(keyanswer)){//do work}

Community
  • 1
  • 1
Josh Engelsma
  • 2,636
  • 14
  • 17
1
if (answer == keyanswer)

this is the wrong programming to compare two string. You need to use this one

if (answer.equals(keyanswer))

or

if(answer.equalsIgnoreCase(keyanswer))
Piyush
  • 18,895
  • 5
  • 32
  • 63