【发布时间】:2019-01-17 22:21:00
【问题描述】:
我刚刚学会了如何编码,我正在尝试创建一个具有注册和登录功能的应用程序。下面的代码将用户名和密码添加到工作正常的文本文件中。
但是,当我尝试使用用户名和密码登录时,verifyLogin 方法不起作用。如果我手动将密码和用户名添加到文本文件中,那么它将正常工作。我最好的猜测是有一些转换错误,但我不确定。
这是在文件中添加用户名和密码的代码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String user = User.getSelectedItem().toString();
String username = Username.getText();
String password = Password.getText();
String passwordConfirm = Password2.getText();
if (user.trim().isEmpty() ||password.trim().isEmpty() || passwordConfirm.trim().isEmpty()){
JOptionPane.showMessageDialog(rootPane, "Please fill out all fields");
}
else if(User.getSelectedItem().equals("Please Select")){
JOptionPane.showMessageDialog(rootPane, "Please select wether you are a student ot a teacher");
}
else if(!password.equals(passwordConfirm)){
JOptionPane.showMessageDialog(rootPane, "Please ensure the passwords you enter match");
}
else{
if(User.getSelectedItem().equals("Student")){
try{
FileWriter writer = new FileWriter("Students.txt", true);
writer.write(System.getProperty("line.separator"));
writer.write(username);
writer.write(",");
writer.write(password);
writer.close();
JOptionPane.showMessageDialog(rootPane, "Success. You now have a students account");
MainGUI x = new MainGUI();
x.setVisible(true);
this.dispose();
}
catch(HeadlessException | IOException e){
JOptionPane.showMessageDialog(rootPane, "Error");
}
}
else{
try{
FileWriter writer = new FileWriter("Teachers.txt", true);
writer.write(System.getProperty("line.separator"));
writer.write(username);
writer.write(",");
writer.write(password);
writer.close();
JOptionPane.showMessageDialog(rootPane, "Success. You now have a teacher account");
MainGUI x = new MainGUI();
x.setVisible(true);
this.dispose();
}
catch(HeadlessException | IOException e){
JOptionPane.showMessageDialog(rootPane, "Error");
}
}
}
}
这是教师登录的代码:
public static void verifyLogin(String username, String password){
boolean found = false;
String tempUsername = "";
String tempPassword = "";
java.io.File file = new java.io.File("Teachers.txt");
try{
Scanner input = new Scanner(file);
input.useDelimiter("[,\n]");
while(input.hasNext() && !found){
tempUsername = input.next();
tempPassword = input.next();
if (tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim())){
found = true;
TeacherOption x = new TeacherOption();
x.setVisible(true);
this.dispose();
}
else{
TeacherLoginError x = new TeacherLoginError();
x.setVisible(true);
this.dispose();
}
}
input.close();
}
catch(FileNotFoundException e){
System.err.format("File does not exist \n");
}
}
【问题讨论】:
-
您的代码将密码写入文件后,您是否查看了该文件?它看起来像您想要的那样吗,尤其是在您显示空白字符的情况下?问题之一是,在写作时您使用的是
line.separator,而在阅读时,您假设行分隔符是\n。请注意,这些可能不是一回事,因此您的分隔符可能无法正常工作,即 Windows 通常将\r\n作为行分隔符,因此您的密码可能以\r结尾。 (顺便说一句,请注意这样做是非常不安全的,所以只需将其用于练习。) -
刚刚检查过,
trim()应该为您摆脱\r(回车)。您是否使用调试器单步调试代码(例如,直接从 IDE 运行代码时)并检查从文件中读取的值? -
@Thomas trim() 去掉了 \r,但它不会阻止 \r 将第一行作为标记。虽然我猜可能还有其他问题。应该逐行读取文件并使用 split() 分隔用户名和密码。