Java 不正确的文件解释/填充

Java 不正确的文件解释/填充,java,file,binary,Java,File,Binary,我刚刚学会了如何编码,我正在尝试创建一个具有注册和登录功能的应用程序。下面的代码将用户名和密码添加到一个正常工作的文本文件中 但是,当我尝试使用用户名和密码登录时,verifyLogin方法不起作用。如果我手动将密码和用户名添加到文本文件中,那么它可以正常工作。我最好的猜测是有一些转换错误,但我不确定 这是将用户名和密码添加到文件的代码: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

我刚刚学会了如何编码,我正在尝试创建一个具有注册和登录功能的应用程序。下面的代码将用户名和密码添加到一个正常工作的文本文件中

但是,当我尝试使用用户名和密码登录时,
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");
    }
} 
在导致问题的文件开头创建换行符。假设您正在输入3
用户名
密码

//empty line
Test1,TestPass1
Test2,TestPass2
Test3,TestPass3
您的数据在扫描仪中是这样读取的

user-1:

pass-1:Test1
user-2:TestPass1

pass-2:Test2
user-3:TestPass1

pass-3:Test3
user-4:TestPass3

pass-4:Test4
user-5:TestPass4
pass-5:
因此造成了麻烦

下面的更改可以解决您的问题,

FileWriter writer = new FileWriter("Students.txt", true);
writer.write(username);
writer.write(",");
writer.write(password);
writer.write(System.getProperty("line.separator"));//add the new line after writing your user credentials
writer.close();
此外,我建议每行读取数据(以避免逻辑错误和更清晰地拾取数据),并尝试以加密格式存储密码

input.useDelimiter("\n");//take input per line
while(input.hasNext() && !found){
    String userDetails=input.next().trim();
    String credentialInfo[]=userDetails.split(",");
    if(!userDetails.isEmpty() 
            && credentialInfo.length==2) {//validate your input
        tempUsername = credentialInfo[0];
        tempPassword = credentialInfo[1];
        if (tempUsername.trim().equals(username.trim()) 
                && tempPassword.trim().equals(password.trim())){
              //rest of your code...

我刚刚又看了一遍你的代码,真不敢相信我竟然没有看到:在while循环中,每当用户名和密码与输入不匹配时,你都会显示
teacherlogin错误

问题出在哪里?如果有一个集合在匹配的集合之前不匹配(这是更常见的情况),那么即使稍后的迭代会找到正确的集合,也会显示错误。因此,首先迭代并查找匹配的用户名和密码,然后在循环之后,根据是否找到内容来决定显示什么

我的意思是:

假设您的文件包含以下内容:

user1,password1
user2,password2
现在我输入“user2”和“password2”。您的代码(我们假设它正确读取了数据)首先根据
user1,password1检查这一点,因为它不匹配,所以您调用else分支:

TeacherLoginError  x = new TeacherLoginError();
x.setVisible(true);
this.dispose();
您需要做什么(重用代码):


在代码将密码写入文件后,您是否查看了该文件?它看起来像是您想要的吗,特别是当您显示空白字符时?其中一个问题是,在编写时使用的是
line.separator
,而在读取时则假定行分隔符是
\n
。注意,这些可能不是同一件事,因此您的分隔符可能无法正常工作,即windows通常使用
\r\n
作为行分隔符,因此您的密码可能以
\r
结尾。(顺便说一句,请注意,这样做是非常不安全的,所以只需将其用于练习。)刚刚选中的
trim()
应该可以为您消除
\r
(回车)。您是否使用调试器(例如,直接从IDE运行调试器时)逐步检查代码并检查从文件读取的值?@Thomas trim()会删除\r,但不会阻止\r将第一行作为标记。不过我想可能还有其他问题。应该逐行读取文件,并使用split()分隔用户名和密码。
//first look for a match only
boolean found = false;
while(input.hasNext() && !found){
  tempUsername = input.next();
  tempPassword = input.next();

  if (tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim())){
     found = true;
     break; //end the loop, you've already found a match
  }
}

//after the loop you act based on whether you found a match or not
if(found) {
  TeacherOption  x = new TeacherOption();
  x.setVisible(true);
} else {
  TeacherLoginError  x = new TeacherLoginError();
  x.setVisible(true);
}

this.dispose(); //this is called anyways so no need to have it twice