2
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

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Samuel
  • 781
  • 5
  • 22

7 Answers7

14

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:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
4

If you want compare string values, you should use the equals() method, as in str.equals(value).

apaderno
  • 28,547
  • 16
  • 75
  • 90
Taruni
  • 1,671
  • 4
  • 22
  • 43
3

This is a common pitfal in java. Basically what aioobe said. Here's the code... It can be tricky. If you do:

 if( "a" == "a" )

You will get true because the compiler will just see two static strings that are equal just 'reuse' one. The == operator for String compares the REFERENCES, meaning it's testing to see if they are the same object. Even a case of:

String a = "a" ;
if (a == "a") {

You'll still get true because again the string gets recycled when the compiler optimizes that code to reuse the first "a" for the second to save space.

Now in the following case, we generate an empty string, manipulate it by appending "a"(not really, strings are immutable, so we end up generating a 3rd string BUT that is a different one since the JVM isn't going to waste its time looking for an existing string that's the same.

class tmp {

public static void main(String arg[]) {
String a = "" ;
a = a+"a" ;

if( a == "a" ) {
    System.out.println("true") ;
}
else {
     System.out.println("false") ;
}

if( "a".compareTo("a") == 0 ) {
    System.out.println("true") ;
}
else {
     System.out.println("false") ;
}

System.out.println("a = '" + a + "'") ;

}

}

Andrew
  • 2,530
  • 16
  • 9
  • Yeah... it would be SOOO much easier if the compiler would just generate a different string every time rather than trying to save space... until the programs get too big... no problem, we put a switch on the compiler to tell it to reuse strings... bump along for months on development, image gets too big, turn it on everything starts breaking.... YAY! Overtime for everyone refactoring until our fingers bleed! (Oh poo, we're salaried) – Andrew May 20 '11 at 12:32
2

Use code.equals(target) instead of code == target

http://bimal4u.wordpress.com/2007/03/29/what-is-the-difference-between-aequalsb-and-a-b/

Asad Iqbal
  • 304
  • 4
  • 16
1

comparing two strings wont work with == . It is only when u compare a value... so plz try something like below

if (strcmp(code, target) == 0)

or

if (code.equals(target))
mandava
  • 336
  • 4
  • 19
1

Replace code == target with code.equals(target)

necixy
  • 4,964
  • 5
  • 38
  • 54
1

You should try the following code, because references of two strings, even if the strings are the same, are not the same.

if (something.equals(someOtherThing)) {
  // …
}
apaderno
  • 28,547
  • 16
  • 75
  • 90
fsonmezay
  • 528
  • 2
  • 10
  • 25