-3

The Toast keeps saying access denied, even if i fill in the right pin.

I need to lock users into an kiosk-mode app, and only with the right pin they will have access to the settings and the exit option.

            public void onClick(View v) {

            AlertDialog.Builder alert = new AlertDialog.Builder(FullscreenActivity.this);
            alert.setTitle("PIN:");
            //alert.setMessage("Message");

            final EditText pinEntry = new EditText(FullscreenActivity.this);
            alert.setView(pinEntry);
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String pin = pinEntry.getText().toString().trim();
                    String secret = "0000";
                    if (pin == secret){
                        Toast.makeText(getApplicationContext(),"Access Approved", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(getApplicationContext(),"Access Denied", Toast.LENGTH_SHORT).show();
                    }
                    Toast.makeText(getApplicationContext(),pin, Toast.LENGTH_SHORT).show();
                }
            });

            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            });
            alert.show();
        }
    });

3 Answers3

7

== tests for reference equality.

.equals() tests for value equality.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
3

Strings are compared with equals() or equalsIgnoreCase(), not with the double equals == like you are using. change your if statement from if(pin == secret) to if(pin.equals(secret)) if they are cap-sensitive, or if(pin.equalsIgnoreCase(secret)) if case does not matter.

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
user2163298
  • 665
  • 4
  • 6
0
public void onClick(DialogInterface dialog, int whichButton) {
                    String pin = pinEntry.getText().toString().trim();
                    String secret = "0000";
                    if (pin.equalsIgnoreCase( secret)){
                        Toast.makeText(getApplicationContext(),"Access Approved", Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(getApplicationContext(),"Access Denied", Toast.LENGTH_SHORT).show();
                    }
                    Toast.makeText(getApplicationContext(),pin, Toast.LENGTH_SHORT).show();
                }
            });