【发布时间】:2022-01-07 23:51:09
【问题描述】:
我正在尝试制作一个没有 MySQL 数据库的系统,该系统将包含帐户和帐户数据。但是没有用
我的账户 java 文件:
/*
* Instead of using 2 different ArrayList (for Username and Password) we used Hashmap
* HashMap stores keys and the keys' values
* In our HashMap keys refer to usernames values refer to passwords
*/
import java.util.HashMap;
import javax.swing.JOptionPane;
public class Accounts {
//I used static to reach list from another class suggest me another way please
static HashMap AccountList = new HashMap<String, HashMap<String, String>>();
public static void CreateAccount(String userID, String password, String email, String fullName, String department){
//We have to check that there shouldn't be an account with the same username
if (AccountList.containsKey(userID)){
JOptionPane.showMessageDialog(null, "There is an account with that username");
}
else{
HashMap Data = new HashMap<String, String>();
Data.put("userID", userID);
Data.put("password", password);
Data.put("email", email);
Data.put("fullName", fullName);
Data.put("department", department);
AccountList.put(userID, Data);
JOptionPane.showMessageDialog(null, "Member created successfully");
}
}
}
在我的 login.java 中,该行出现错误
boolean IsLoginSuccessful = false;
//We are getting username and password text/string from the form
String userID = username_field.getText();
String password = password_field.getText();
//Now check if username is in list then check if username's password equals to password
if (Accounts.AccountList.containsKey(userID)){
String userPassword = Accounts.AccountList.get(userID).get("password");
IsLoginSuccessful = true;
}
第二个“.get”给出了“找不到符号”之类的错误。为什么将 hashmap 作为对象元素?另外,如果您有其他方法可以在没有哈希表的情况下存储这些数据并检查密码等。请告诉我,谢谢。
【问题讨论】:
-
哦,打错字了,但还是一样的语法错误
-
你有原始类型。它的
Map<String, String> accountList = new HashMap<>();- 然后它工作正常。并不是说这是好的代码,但我们都必须从某个地方开始。
标签: java authentication hashmap