1

I'm having a slight problem that i can't understand, i'm building a Web Server that handles calls in the java E.G go to use /SendCommand.html then Java will handle the request, i have a login system built using post, but for some reason my login check is not working,

private boolean checkLogin(String user, String pass){
    for(int i = 0; i < users.users.length; i++ ){
        String test = SHA1.toSHA1(pass);
        if(users.users[i][0] == user && users.users[i][1] == test ){
            return true;
        }
    }
    return false;
}

I'm Breaking at the if statment to provide the information below When i debug this i get,

Name            | Type            | Value
users             Users             #163
  users           String[]          #165(length=1)
    [0]           String[]          #167
      [0]         String            "Admin"
      [1]         String            "d033e22ae348aeb5660fc2140aec35850c4da997"
user              String            "Admin"
pass              String            "admin"
test              String            "d033e22ae348aeb5660fc2140aec35850c4da997"

As you can see users.users[0][0] == user and users.users[0][1] == test why is it returning false from the method?

toasted_flakes
  • 2,502
  • 1
  • 23
  • 38
Barkermn01
  • 6,781
  • 33
  • 83
  • it's not a duplicate im looking for help with a specific problem in my code not what's the difference between .equals and == i did not think it would make a difference in Java like it does not in most Objecting C langs – Barkermn01 Mar 24 '13 at 16:20

3 Answers3

6

Don't use == to compare strings. Use s1.equals(s2) instead. The former compares references, which is almost always not what you want. The latter, on the other hand, compares character sequences.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Thanks for all the details i think that it's flaming retarded that a C language does not support string comparison the same why most Objective C languages do, thanks for this – Barkermn01 Mar 24 '13 at 16:19
0

Use .equals() to compare strings, not ==.

Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0
if(users.users[i][0].equals(user) && users.users[i][1].equals(test))

Always compare String with .equals()

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39