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:
我正在编写一个简单的GUI程序,我必须为用户输入登录名和密码,然后检查他是否在数据库中(我的Hashmap)。那就是我有问题的地方。我通过containsKey检查登录是否正确,并通过containsValue检查密码。问题是,如果在示例中我有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和密码“编程”,程序说,这是正确的。
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 :)
非常感谢任何线索或解决方案:)
1 个解决方案
#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
使用userEnteredPassword.equals ....而不是hashMap.get(userEnteredUsername).equals ...很重要,因为如果userEnteredUsername没有map条目,你将得到一个空指针异常
#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
使用userEnteredPassword.equals ....而不是hashMap.get(userEnteredUsername).equals ...很重要,因为如果userEnteredUsername没有map条目,你将得到一个空指针异常