1

I'm writing a simple GUI program where I have to input login and password for user and then check if he is in database (my Hashmap). That's where i have problem. I checked if login is correct by containsKey and checked for password by containsValue. The problem is, that if in example I have 2 users:

login: user1 password: example login: user2 password: programing 

If I put login as user1 in my JTextField and password "programing", the program says, that it's correct.

That's where I have a problem. How to check the password for exact login, which is key?

Would be very thankful for any clues or solutions :)

0

1 Answer 1

2

You can use a check like this:

 if(userEnteredPassword.equals(hashMap.get(userEnteredUsername))){ // Correct credentials. ..... } 

It's important to use userEnteredPassword.equals.... instead of hashMap.get(userEnteredUsername).equals... because you'll get a null pointer exception if there is no map entry for userEnteredUsername

Sign up to request clarification or add additional context in comments.

6 Comments

...or use the Objects.equals method.
It really helped alot. Thank's for your feedback :)
@PawełGustawKolasiński I'm glad I could help. Good luck.
but what if 2 users have the same password? Then with this method I get message, that the password is wrong.
Nevermind, I forgot to write 1 thing in if(), now everything works perfectly :)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.