0

i have a username and a password string. How can I compare them in Android? This is somewhat what I'm looking for:

if( user== username && pass == password){ then do this  }
else if(user[does not equal to]username && pass==password{ toast (invaild username.)}
else if(user==username && pass[does not equal] password{toast(invaild password)}
else{ toast [invaild login]}

I know that's not correct coding but the two "else if" statements is what I'm looking for. The rest is just to give a better understanding of what I'm trying to do.

The username is what's entered into a Edittext same with the Password so they are both String when i .getText.toString right?

Dakota Miller
  • 483
  • 1
  • 4
  • 21
  • 1
    First of all: strings have to be compared using `.equals()`, not `==`. As to what I believe is your question: don't tell the user whether his username or password is wrong. If you do so, it will encourage people to guess a username + password combination. Just displaying 'invalid username/password' should be enough. – Jeroen Vannevel Dec 16 '13 at 00:52
  • Good thought, this is for work tho and the people their wont really have the time nor the knowledge for a gussing game. – Dakota Miller Dec 16 '13 at 00:55
  • Okay, so what is it exactly you're asking? – Jeroen Vannevel Dec 16 '13 at 00:55
  • If you care about security you should not store passwords... – assylias Dec 16 '13 at 01:00

4 Answers4

2

To compare the text in strings use equals() e.g. pass.equals(password) rather than == which checks if two strings are the same object.

For "does not equal" you can negate the statement e.g. !pass.equals(password).

Jeroen Vannevel
  • 42,521
  • 22
  • 100
  • 163
splrs
  • 2,404
  • 2
  • 18
  • 28
0

You could use pass.equal(password).

apaderno
  • 26,733
  • 16
  • 74
  • 87
0

Use the equals method for comparing strings.

if( user.equals(username) && pass.equals(password))
{
    then do this 
}
else if(!user.equals(username) && pass.equals(password)
{  
 Toast.makeText(this,"invalid username",Toast.LENGTH_SHORT).show();
}
else if(user.equals(username) && !pass.equals(password))
{
Toast.makeText(this,"invalid password",Toast.LENGTH_SHORT).show();
 }
else{ 
Toast.makeText(this,"Invalid login",Toast.LENGTH_SHORT).show(); 
}
Luis Pena
  • 3,815
  • 2
  • 13
  • 23
0

You want to use .equals(..) to compare Strings. Also, to do not logically, you can use !.

//define user, username
//define pass, password
if(user.equals(username) && pass.equals(password)){ 
    //then do this  
}
else if(!user.equals(username) && pass.equals(password)){
    //toast (invaild username.)
    Toast.makeText(getApplicationContext(), "invalid username", Toast.LENGTH_SHORT).show();
}
else if(user.equals(username) && !pass.equals(password)){
    //toast(invaild password)
    Toast.makeText(getApplicationContext(), "invalid password", Toast.LENGTH_SHORT).show();
}
else{ 
    //toast [invaild login]
    Toast.makeText(getApplicationContext(), "invalid login", Toast.LENGTH_SHORT).show();
}

Here are some sources on Toast:

http://www.mkyong.com/android/android-toast-example/

http://developer.android.com/guide/topics/ui/notifiers/toasts.html