好吧,我做了一個更簡單的方式來存儲用戶名和密碼數據。當用戶提交註冊表格,它會輸出到一個文本文件
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();
}
}
但是當我運行該程序,即使它們正確,它仍會報告錯誤的用戶名或密碼。如何解決這個問題?
不,請不要這樣做。 – duffymo 2013-02-28 00:07:43
@duffymo並做什麼呢? – OscarRyz 2013-02-28 00:09:48
如果你熟悉C++,那麼你知道它的類作爲結構幾乎是相同的。 Java只有類。 – 2013-02-28 00:17:51