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



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

数据

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);
}

您需要分开缓冲读取器,然后使用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(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上,因为它没有semicolon感谢您尝试修复它的Guyss:)

最新更新