我目前正在从事由老师分配给我的项目,我需要使用以下方式将用户登录到我的系统中: 首先,用户使用arraylist(必须包含分配的一部分)将数据注册到文件中,这部分工作正常,我使用的代码是
ArrayList<data> regUser = new ArrayList<data>();
public void regUser() {
String regName = JOptionPane.showInputDialog(null,"Enter User Name:");
String regPass = JOptionPane.showInputDialog(null,"Enter User Password:");
data p = new data(regName,regPass);
regUser.add(p);
for (int i = 0; i < regUser.size(); i++) {
try {
FileWriter writer = new FileWriter("C:\\Users\\Desktop\\outputFile.txt",true);
PrintWriter pw = new PrintWriter(writer);
pw.println(regUser.get(i));
pw.close();
writer.close();
} catch (IOException e) {
System.out.println("File not found");
}
}
JOptionPane.showmessagedialog(null,"User registered successfully");
}
现在用户必须以相同的方式从文件中的数据登录,首先将文件中的数据输入到arraylist中,然后将其与用户输入的数据匹配,但是出现问题;
public void login() {
ArrayList<String> loginUser = new ArrayList<>();
String name = JOptionPane.showInputDialog(null,"Enter User Name:");
String password = JOptionPane.showInputDialog(null,"Enter User Password:");
try {
FileReader fr = new FileReader("C:\\Users\\Desktop\\outputFile.txt");
BufferedReader br = new BufferedReader(fr);
String st;
while ((st = br.readLine()) != null) {
loginUser.add(st);
}
// System.out.println(loginUser);
br.close();
fr.close();
} catch (IOException e) {
System.out.println("File not found");
}
for (int i = 0; i < loginUser.size(); i++) {
if (loginUser.get(0).equals(name) && loginUser.get(1).equals(password)) {// help need for find a condition here
JOptionPane.showmessagedialog(null,"Welcome to Rent A Car");
return;
}
}
JOptionPane.showmessagedialog(null,"Wrong login info please try again");
}
我不是arraylist的专家,所以如果有人可以指导我,我将非常感谢 现在,我的outputfile.txt中的数据是: Helo 123 测试456