好的,我做了一个更简单的方法来存储用户名和密码数据。当用户提交注册表单时,它会输出到一个文本文件中
private class submitListener implements ActionListener {
public void actionPerformed(ActionEvent e){
try {
String newEmail, newUser, systemMsg;
char[] newPass;
char[] confirmNewPass;
newEmail = emailField.getText();
newUser = usernameField.getText();
newPass = passwordField.getPassword();
confirmNewPass = confirmPassField.getPassword();
String strPass = new String(newPass);
String strConfPass = new String(confirmNewPass);
if(strPass.equals(strConfPass) && !newEmail.isEmpty() && !newUser.isEmpty() &&
!strPass.isEmpty()){
BufferedWriter out = new BufferedWriter(new FileWriter("Data.txt", true));
BufferedWriter emailOut = new BufferedWriter(new FileWriter("email.txt", true));
out.write(newUser);
out.write("\t\t");
out.write(newPass);
out.write(System.getProperty("line.separator"));
emailOut.write(newEmail);
emailOut.write(System.getProperty("line.separator"));
systemMsg = "New account created, please log in.";
JOptionPane.showMessageDialog(null, systemMsg);
((CardLayout)cardPanel.getLayout()).show(cardPanel, "login");
emailField.setText(null);
usernameField.setText(null);
passwordField.setText(null);
confirmPassField.setText(null);
out.close();
emailOut.close();
}
else{
systemMsg = "Please enter the missing information.";
JOptionPane.showMessageDialog(null, systemMsg);
}
}catch(IOException E){}
}
}
然后它将登录信息与这些现有的文本文件进行比较,如下所示:
private class LoginListener implements ActionListener {
public void actionPerformed(ActionEvent e){
String username, inUsername, inPass, str = "Incorrect username or password.";
char[] password;
username = usernameField.getText();
password = passwordField.getPassword();
//Convert char to str
String convertPass = new String(password);
try {
fileScan = new Scanner(new File("Data.txt"));
}catch(Exception err){}
while(fileScan.hasNext()){
inUsername = fileScan.next();
inPass = fileScan.next();
if(username.equals(inUsername) && convertPass.equals(inPass)){
((CardLayout)cardPanel.getLayout()).show(cardPanel, "main");
counter++;
}
}
if(counter == 0){
JOptionPane.showMessageDialog(null, str);
usernameField.setText(null);
passwordField.setText(null);
}
fileScan.close();
}
}
但是当我运行该程序时,即使它们是正确的,它也会不断报告错误的用户名或密码。如何解决这个问题?