当我将哈希映射的值与 JtextField 文本字符串进行比较时,我得到 false。为什么 if 语句返回 false?
public void actionPerformed(ActionEvent e) {
if(LoginOperations.Login(Integer.valueOf(customerNumberField.getText()),
passwordField.getText())) {
public class LoginOperations {
public static HashMap<Integer, String> loginCredentials = new HashMap<Integer, String>();
public static void CreateDummyLoginData() {
loginCredentials.put(123451, "password1");
loginCredentials.put(123452, "password2");
loginCredentials.put(123453, "password3");
loginCredentials.put(123454, "password4");
}
public static boolean Login(int customerNumber, String password) {
//to do: masking the password with hash table
CreateDummyLoginData();
String debug = loginCredentials.get(customerNumber);
if(loginCredentials.get(customerNumber) == password) {
return true;
}
else {
return false;
}
}
当我修改代码并传递硬编码密码作为参数时,它可以工作并返回 true
public void actionPerformed(ActionEvent e) {
if(LoginOperations.Login(Integer.valueOf(customerNumberField.getText()),
"password3")) {
我尝试比较哈希图的密码值和输入的 JTextFiled 值。
我使用 equals() 函数解决了这个问题。 == 运算符也比较内存地址。所以它没有起作用。它不符合我的想法,但使用 equals() 解决了我的问题。