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,996
  • 7
  • 44
  • 62
  • 1
    Do `System.out.println( ">" + userPassword + "<");` and same for `mapPassword` to ensure there is no leading space around – azro Jul 04 '21 at 10:41

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
  • 568
  • 3
  • 5