我目前正在由老师分配给我的项目中,我需要使用来将用户登录到我的系统中;首先,用户使用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
用以下代码替换for
中的login()
循环:
for (int i = 0; i < loginUser.size(); i++) {
String[] credentials = loginUser.get(0).split(" ");
if (credentials[0].equals(name) && credentials[1].equals(password)) {
JOptionPane.showMessageDialog(null, "Welcome to Rent A Car");
return;
}
}
假设:文件的内容,outputFile.txt
如下]
helo 123
test 456
说明:从文件中读取一行时,它包含name
和password
两者的值,并用" "
分隔。因此,您需要将该行分为两部分(索引0
处的name值和索引1
处的password值)。
如果您有任何疑问,请随时发表评论。
您正在正确地从文件中获得该行,但是您必须拆分该行的各个部分(在这种情况下,请使用空格分隔),以便分别获取用户名和用户密码。首先,您可能希望在文本文件的每一行上都有用户名和密码,以便可以在while
循环中为每个读取行分别提取1个用户名和1个密码,例如的outputfile.txt
:
helo 123
test 456
我们假设这将始终按预期运行(=文件中的一行将始终仅包含用户名和密码,并用空格字符分隔)。在一个严肃的项目中,您绝对不应该假定那样做,而应该使用某种数据存储。
然后您可以像这样在while循环中实现每一行的拆分:
String[] credentials = st.split(" ");
[credentials
将包含2个元素,即用户名和密码(例如,凭据[0] =“ helo”和凭据[1] =“ 123”)。
然后您可以在while
循环中执行以下操作,以将凭据添加到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)
{
// This changed
String[] credentials = st.split(" ");
for (String credential : credentials) {
loginUser.add(credential);
}
}
br.close();
fr.close();
}
catch(IOException e)
{
System.out.println("File not found");
}
// This for block changed
for (int i = 0; (i + 1) < loginUser.size(); i += 2) {
String uname = loginUser.get(i);
String pass = loginUser.get(i + 1);
if (uname.equals(name) && pass.equals(password)) {
JOptionPane.showMessageDialog(null, "Welcome to Rent A Car ");
return;
}
}
JOptionPane.showMessageDialog(null, "Wrong login info please try again");
}
上面的代码假设您的用户名是文件中的第一组字符,密码是文件中的第二组字符,其中用户名和密码用空格字符分隔。 i
循环中的for
递增2,因为0 =用户名,1 =密码; 3 =用户名,4 =密码,等等。(i + 1) < loginUser.size();
这是一个很讨厌的代码,带有ArrayList
,但是如果这是分配的必需项,那就这样吧。
希望这会有所帮助!