Username & password string comparison problem
When I enter "abcd" in the passwordField, and use System.out.println(s21) - to chec开发者_运维技巧k the password - it shows "[C@1372a1a", why?
private void submit() {
String s1 = userNameField.getText();
char[] s2 = passwordField.getPassword();
String s21 = s2.toString();
if (s1.equals(management.getUsernamesAdmin())&& s21.equals(management.getPasswordsAdmin())) {
SystemManagementPage admin = new SystemManagementPage(this, true, management);
admin.setVisible(true);
}
}
}
Your problem is here:
String s21 = s2.toString();
This is calling toString()
on a char[]
, and isn't going to return anything useful. Instead,
String s21 = new String(s2);
Will give you a String composed of the characters in the array.
Other than that, I can't see any reason your code shouldn't work.
char[] s2 = passwordField.getPassword();
String s21 = s2.toString();
s2 is an Object of type char[]
s2.toString() returns the string representation of the s2 object, as implemented by java.lang.Object
... getClass().getName() + '@' + Integer.toHexString(hashCode())
My suggestions:
- after performing
String s21 = s2.toString();
what is the value of s21? - check with a debugger whether you reach that line inside the if condition
SystemManagementPage admin = new SystemManagementPage(this, true, management);
While you assume the password value is the problem, problem might be atadmin.setVisible(true);
or anywhere else. - Make sure your passwords are stored in clear text, since this is what you are trying to do here compare them directly and not their hash or encrypted value.
- Make sure your using same case
Admin != admin
- Does your
passwordField
has a.getText()
method as well?
精彩评论