0

I have a string value stored in a variable called userPassword, which is what I got from the user using scanner. I also have a hashmap like this:

Key Value
username1 password1
username2 password2
username3 password3

I have a variable userPassword with value 123, and also a value in hashmap which is also 123

public class Authentication {
    public void verifyLogin(LoginDetailsPojo userLoginDetailsObj, Map<String,String> map){
    //Passing a object and hash map as parameters
        String userPassword = userLoginDetailsObj.password;
        System.out.println(userPassword);//Printing "123"
        String mapPassword = map.get(userLoginDetailsObj.userName);
        System.out.println(mapPassword); //Printing "123"
        if(userPassword.equals(mapPassword))
            System.out.println("it is equal");
        }
    }
}

Even though both the variables (userPassword and mapPassword) have the same value 123, the if block is not executing

Pawel Veselov
  • 3,808
  • 6
  • 42
  • 59

1 Answers1

0

try this, I have added trim() to delete any head or trail white spaces

public class Authentication {
    public void verifyLogin(LoginDetailsPojo userLoginDetailsObj, Map<String,String> map){
    //Passing a object and hash map as parameters
        String userPassword = userLoginDetailsObj.password;
        System.out.println(userPassword);//Printing "123"
        String mapPassword = map.get(userLoginDetailsObj.userName);
        System.out.println(mapPassword); //Printing "123"
        if(userPassword.trim().equals(mapPassword.trim()))
            System.out.println("it is equal");
        }
    }
}
anish sharma
  • 527
  • 2
  • 5