Search code examples
javaif-statementruntime-errorbufferedreader

Code suddenly stop after reading data from text file


There is no error, but the code Stop after getting the username and password from text file and don't even read the next line for checking the role of the user could you help me how to fix this kind of error

Data

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

this is what in the 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);
}

Solution

  • You need to separate your buffered reader then use a for loop to get your required outcome. This should stop the error.

    I'd suggest first storing the file into an array list.

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

    Now convert the array list into a String Array using:

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

    Then create a String from the String array:

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

    Now you can manipulate this string and split it into a new split_content array.

         String[] getdata = new String[0];
    
         getdata = str_file_contents.split(";");
    

    Here is where you use the for loop for your problem:

    Below is pseudocode that has NOT been tested and should be used for educational purposes only.

    I've included this for loop due to the fact that I wasn't sure if you wanted it to pick up on the user entering different username or password combinations.

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

    Key problem here. You used:

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

    String arrays start at 0 and therefore Uname.equals(getdata[1]) will be a password and Psd.equals(getdata[2]) will be the second username, this will cause issues, I have fixed above. Check the pseudocode.