-1

I'm trying to do the most basic form of security at the start of my app, where if you put in the right name and surname, a new activity will be called.

See my code here:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button buttonIntroSubmit = (Button) findViewById(R.id.button_intro);

    Intent introPass = new Intent(this, questionareActivity.class);

    buttonIntroSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            EditText firstName = (EditText) findViewById(R.id.first_name);
            EditText lastName = (EditText) findViewById(R.id.last_name);
            TextView test = (TextView) findViewById(R.id.test);

            String fullName = firstName.getText() + " " + lastName.getText();
            String password = "Mieke Snyman";

            test.setText(fullName);

            if(fullName !== password) {
                String toastPass = "Approved";
                Toast pass = Toast.makeText(getApplicationContext(), toastPass, Toast.LENGTH_LONG);
                pass.show();
            }
        }
    });
}

If I type in exactly "Mieke" at the firstName field and exactly "Snyman" at the lastName, it shows "Mieke Snyman" at the test TextView which I assigned the text content via variable "fullName". But if I use that same variable for the if condition it doesn't go into the if.

Any ideas why? Maybe a white space character or something i'm missing in my if condition?

Wilhelm
  • 113
  • 6

2 Answers2

1

http://www.tutorialspoint.com/java/java_basic_operators.htm

 if(fullName !== password) { // !== we don't have any opertor like this in java. 
                    String toastPass = "Approved";
                    Toast pass = Toast.makeText(getApplicationContext(), toastPass, Toast.LENGTH_LONG);
                    pass.show();
                }

Instead you can do it like this

if (!fullName.equals(password){


}

http://perso.ensta-paristech.fr/~diam/java/online/notes-java/data/expressions/22compareobjects.html

Anil Meenugu
  • 1,411
  • 1
  • 11
  • 16
1

Use .equals(); instead of == so it would look like this:

if (!fullName.equals(password){
String toastPass = "Approved";
                Toast pass = Toast.makeText(getApplicationContext(), toastPass, Toast.LENGTH_LONG);
                pass.show();
}

Important Info == tests for reference equality. .equals() tests for value equality.

and as you can see String fullName doesn't have the same reference as String password which will result false, note that == works perfectly with primitives; simply because they aren't objects unlike Strings in java.

More useful info.

Documentation.

Edit

Talking about security I can notice that you are using a String to save the password which is a bad practice in java.

Once you create a string you can't get rid of the data in the memory as Strings are immutable, So its preferable to use char[]

Ahmad Sanie
  • 3,678
  • 2
  • 21
  • 56