-3

Hi I am making a logo quiz game and my if statement does not react to my input I made a textview and I pirnted out the input and it was yahoo (i entered yahoo in edit text and i declered if statement to check if it is yahoo)...

package com.example.logogame;

imports....

public class Yahoo extends Activity implements OnClickListener{

EditText et;
Button check;
TextView test;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yahoo);

    et = (EditText) findViewById(R.id.editTextAnswer);
    check = (Button) findViewById(R.id.buttonCheck);
    test = (TextView) findViewById(R.id.test);

    check.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.buttonCheck:

        String content = et.getText().toString();

        //its always wrong here...
        if(content == "yahoo"){
            Toast toast = Toast.makeText(this, "Correct! Good work", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }else{
            test.setText(content);
            Toast toast = Toast.makeText(this, "Wrong...", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }

        break;

    }
}

3 Answers3

1

To compare Strings use .equals().

So Change

if(content == "yahoo"){

to

if(content.equals("yahoo")){

For more info see How do I compare strings in Java?

Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
1

Used .equals() method for String comparison

  if(content.equals("yahoo"))
M D
  • 47,665
  • 9
  • 93
  • 114
0

.equals("String name ") use this for string comparison.