使用来自文件Java代码的arraylist进行登录?

我目前正在从事由老师分配给我的项目,我需要使用以下方式将用户登录到我的系统中: 首先,用户使用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

junqiou 回答:使用来自文件Java代码的arraylist进行登录?

您可以正确地从文件中获取行,但是必须拆分行的各个部分(在这种情况下,请按空格分隔),以便分别获取用户名和用户密码。首先,您可能希望在文本文件的每一行上都包含用户名和密码,以便在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的令人讨厌的代码,但是如果这是分配的必需项,那就这样吧。

希望这会有所帮助!

,

用以下代码替换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

说明::当您从文件中读取一行时,该行包含namepassword的值,并用" "分隔。因此,您需要将该行分为两部分(索引0处的name值和索引1处的password值)。

如果您有任何疑问,请随时发表评论。

本文链接:https://www.f2er.com/2990900.html

大家都在问