[英]How to check if value is correct for key in HashMap in Java
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). 我正在编写一个简单的GUI程序,我必须在其中输入用户的登录名和密码,然后检查他是否在数据库中(我的Hashmap)。 That's where i have problem. 那就是我有问题的地方。 I checked if login is correct by containsKey and checked for password by containsValue. 我通过containsKey检查登录是否正确,并通过containsValue检查密码。 The problem is, that if in example I have 2 users: 问题是,如果在示例中我有2个用户:
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. 如果我以user1
身份在我的JTextField
输入登录名,并输入密码“ programming”(编程),则程序会说这是正确的。
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 :) 非常感谢任何线索或解决方案:)
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
使用userEnteredPassword.equals....
而不是hashMap.get(userEnteredUsername).equals...
很重要,因为如果没有userEnteredUsername
映射条目, userEnteredUsername
获得空指针异常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.