从文本文件读取数据后代码突然停止

Code suddenly stop after reading data from text file

没有错误,但代码在从文本文件中获取用户名和密码后停止,甚至不读取下一行来检查用户的角色 你能帮我解决这种错误吗

数据

Bill;admin;admin;Admin 
Miguel;SMMiguel;SM1234;Sales Manager 
Josh;PMJosh;PM1234;Purchase Manager 

这是 User.txt

中的内容
 private void initialize() {
    Login_Frame = new JFrame();
    Login_Frame.setTitle("Login");
    Login_Frame.setBounds(100, 100, 471, 415);
    Login_Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Login_Frame.getContentPane().setLayout(null);

    User_txt = new JTextField();
    User_txt.setBounds(145, 93, 216, 22);
    Login_Frame.getContentPane().add(User_txt);
    User_txt.setColumns(10);

    JButton Login_btn = new JButton("Log in");
    Login_btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            // name of the file
            String fileName = "User.txt";
            String Uname = User_txt.getText();
            String Psd = Pass_txt.getText();
            Item_Entry ie = new Item_Entry();
            ie.set_box(Uname);
            String line = null;
            String SMrole = "Sales Manager";
            String PMrole = "Purchase Manager";

             try {
                 //read and buffer the file
                    FileReader fileReader = new FileReader(fileName);
                    BufferedReader br = new BufferedReader(fileReader);

                    while ((line = br.readLine()) !=null) 
                    {

                        String[] getdata = line.split(";");

                        if(Uname.equals(getdata[1]) && Psd.equals(getdata[2]))
                        {
                            JOptionPane.showMessageDialog(null, "Get the data for user and pass");
                            if(PMrole.equals(getdata[3]))
                            {
                                JOptionPane.showMessageDialog(null, "Login Successful");
                                Purchase_Manager_Access PMAccess = new Purchase_Manager_Access();
                                PMAccess.setVisible(true);
                                Username = User_txt.getText();
                            }

                            else if(SMrole.equals(getdata[3]))
                            {
                                Sales_Manager_Access SMAccess = new Sales_Manager_Access();
                                SMAccess.setVisible(true);
                                Username = User_txt.getText();

                            }

                        }
                    }
                }
                    catch (FileNotFoundException ex) {
                        System.out.println("Unable to open file '" + fileName + "'");     
                    }
                    catch(IOException ex) {
                        System.out.println("Error writing to file '" + fileName + "'");
                    }

        }
    });
    Login_btn.setBounds(94, 210, 109, 49);
    Login_Frame.getContentPane().add(Login_btn);

    JButton Exit_btn = new JButton("Exit");
    Exit_btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    Exit_btn.setBounds(239, 210, 109, 49);
    Login_Frame.getContentPane().add(Exit_btn);

    JLabel lblUsername = new JLabel("Username");
    lblUsername.setBounds(59, 96, 78, 16);
    Login_Frame.getContentPane().add(lblUsername);

    lblPassword = new JLabel("Password");
    lblPassword.setBounds(59, 145, 56, 16);
    Login_Frame.getContentPane().add(lblPassword);

    Pass_txt = new JPasswordField();
    Pass_txt.setBounds(145, 142, 216, 22);
    Login_Frame.getContentPane().add(Pass_txt);
}

您需要将缓冲的 reader 分开,然后使用 for 循环来获得所需的结果。这应该会停止错误。

我建议先将文件存储到数组列表中。

BufferedReader in = new BufferedReader(new FileReader(file_name));//replace file_name with your "User.txt"

String line_reader = null; // this is the string that reads each line

while((line_reader = in.readLine()) != null) // loop to add content to array list

  {
    file_content_list.add(line_reader);
  }

现在使用以下方法将数组列表转换为字符串数组:

    String[] file_contents = new String[0];

     // CONVERSION ARRAY LIST -> ARRAY
    file_contents = file_content_list.toArray(new String[0]);

然后从字符串数组创建一个字符串:

    // CONVERSION ARRAY -> STRING
    String string_file_content = null;
    string_file_content = Arrays.toString(file_contents);

现在您可以操作此字符串并将其拆分为一个新的 split_content 数组。

     String[] getdata = new String[0];

     getdata = str_file_contents.split(";");

这里是你使用 for 循环解决问题的地方:

以下是未经测试的伪代码,应仅用于教育目的。

我已经包含了这个 for 循环,因为我不确定您是否希望它在用户输入不同的用户名或密码组合时接收。

for(int i = 0,j=1; i <= string_file_content.length; i++, j++)
   {
      if(Uname.equals(getdata[i]) && Psd.equals(getdata[j]))
          {
            JOptionPane.showMessageDialog(null, "Get the data for user and pass");
                        if(PMrole.equals(getdata[3]))
                        {
                            JOptionPane.showMessageDialog(null, "Login Successful");
                            Purchase_Manager_Access PMAccess = new Purchase_Manager_Access();
                            PMAccess.setVisible(true);
                            Username = User_txt.getText();
                        }

                        else if(SMrole.equals(getdata[3]))
                        {
                            Sales_Manager_Access SMAccess = new 
                            Sales_Manager_Access();
                            SMAccess.setVisible(true);
                            Username = User_txt.getText();

                         }

关键问题在这里。您使用了:

if(Uname.equals(getdata[1]) && Psd.equals(getdata[2]))

字符串数组从 0 开始,因此 Uname.equals(getdata[1]) 将是密码,Psd.equals(getdata[2]) 将是第二个用户名,这会导致问题,我已在上面修复。检查伪代码。

伙计们,我设法修复了它,问题出在 User.txt 因为它没有分号所以不能将它拆分成数组并读取它 感谢您尝试修复它 guysss :)