Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if("a" == "a") not working [duplicate]

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.beta);

    ed_code = (EditText) findViewById(R.id.et_beta_01);
    bu_ok = (Button) findViewById(R.id.bu_beta);

    bu_ok.setOnClickListener(new OnClickListener() {    
        @Override
        public void onClick(View v) {
                // TODO Auto-generated method stub
            String code = ed_code.getText().toString();
            String target = "vsi8";

            Log.v(TAG, code+"="+target);

            if(code == target){
                    Intent intent = new Intent(BetaCheck.this, AppMenu.class);
                startActivity(intent);
            }
            else {
                    Toast.makeText(getApplicationContext(), "wrong", Toast.LENGTH_SHORT).show();
                    ed_code.setText("");
            }
        }
    });
}

It seems that the the if statement does not understand that the 2 values are equal.

Thanks for the help

like image 445
Samuel Avatar asked Aug 08 '26 00:08

Samuel


1 Answers

Strings, should be compared using .equals and not ==. (== checks for reference equality and not for content equality.)

That is, change

if(code == target)

to

if(code.equals(target))

Related question:

  • How do I compare strings in Java?
like image 117
aioobe Avatar answered Aug 09 '26 13:08

aioobe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!