Java JTextField and hashmap string value comparing

18 Views Asked by At

I get false when I compare the value of a hash map with JtextField text string. Why is this returns false to the if statement?

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;
        }
    }

When I modify the code with passing hardcoded password as parameter, it works and returns true

public void actionPerformed(ActionEvent e) {
                if(LoginOperations.Login(Integer.valueOf(customerNumberField.getText()), 
                        "password3")) {

I tried to compare the password value of an hashmap and inputted JTextFiled value.

1

There are 1 best solutions below

0
mlkmhmtbyk On

I resolved it with using equals() function. == operator compares the memory addresses too. So it did not worked. It doesn't fit to my mind but using equals() resolved my problem.