gpt4 book ai didi

java - 文件解释/填充不正确

转载 作者:行者123 更新时间:2023-12-02 10:55:31 25 4
gpt4 key购买 nike

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

但是,当我尝试使用用户名和密码登录时,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...

关于java - 文件解释/填充不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51784167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com