Search code examples
javajframejoptionpanejdialog

Wrong Behavior with my JFrame and JDialog Login System


I have implemented a basic login system that splits up a text file (first name : last name : City : username : password)

Harold : Fisher : San Francisco : hf45 : 1234

When the user inputs his/her user credentials, it will display a JFrame if credentials are entered correctly, otherwise a JOptionPane will appear instead informing the user that the credentials entered are incorrect.

Here is my code:

public class LoginFrame extends javax.swing.JFrame      {

/**
 * Creates new form LoginFrame
 */ 
String username;
String password;
String filePath = "UserDetails.txt";

public LoginFrame() {
    initComponents();


private void jButtonLoginActionPerformed(java.awt.event.ActionEvent evt) {                                             

        username = jTextFieldUsername.getText();
        password = jTextFieldPassword.getText();
        verifyLogin();  
}                                            

public void verifyLogin()
{
   try {
       File f = new File("UserDetails.txt");
       Scanner fileRead = new Scanner(f);

       while(fileRead.hasNextLine())
       {
           String textLine = fileRead.nextLine();
           String[] userDetails = textLine.split(" : ");
           String tempUsername = userDetails[3];
           String tempPassword = userDetails[4];

           if(tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim()))
                   {
                       new LibraryCatalogFrame().setVisible(true);
                   }
           else
           {
               JOptionPanes.messageBox("Please re-enter your user details", "Incorrect Username or Password");
         }
        }
       }
       catch (FileNotFoundException e)
               {
               JOptionPanes.messageBox("Error", "FileNotFound");
          }
       }

The problem I am facing right now is that when the user correctly inputs his credentials, both the JFrame and JOptionPane are displayed. Also, when the user inputs his credentials incorrectly, the JFrame does not display (as intended), but the JOptionPane appears twice instead of just once.


Solution

  • I observed the behavior you explain when UserDetails.txt file has two lines. E.g.

    Harold : Fisher : San Francisco : hf45 : 1234
    John : Snow : San Francisco : js45 : 5678

    With above file with 2 lines, below "Program with the problem" outputs the behavior you explain (both "Show JFrame" and "Show dialog" are printed).

    Problem is that, in the while loop, you try to show JFrame or dialog for every line in the file.

    Try below "Fixed program". In that, I'm using a boolean variable matched to store whether a match is found. And then show the JFrame or dialog after the while loop.

    Program with the problem:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class LoginFrame
    {
      // Hardcode user entered username and password to simplify the program
      String username = "hf45";
      String password = "1234";
    
      public static void main(String[] args)
      {
        new LoginFrame().verifyLogin();
      }
    
      public void verifyLogin()
      {
        try {
          File f = new File("UserDetails.txt");
          Scanner fileRead = new Scanner(f);
    
          while(fileRead.hasNextLine())
          {
            String textLine = fileRead.nextLine();
            String[] userDetails = textLine.split(" : ");
            String tempUsername = userDetails[3];
            String tempPassword = userDetails[4];
    
            if(tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim()))
            {
              //new LibraryCatalogFrame().setVisible(true);
              System.out.println("Show JFrame");
            }
            else
            {
              System.out.println("Show dialog");
              //JOptionPanes.messageBox("Please re-enter your user details", "Incorrect Username or Password");
            }
          }
        }
        catch (FileNotFoundException e)
        {
          e.printStackTrace();
          //JOptionPanes.messageBox("Error", "FileNotFound");
        }
      }
    }
    

    Output:
    Show JFrame
    Show dialog

    Fixed program:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class LoginFrame
    {
      // Hardcode user entered username and password to simplify the program
      String username = "hf45";
      String password = "1234";
    
      public static void main(String[] args)
      {
        new LoginFrame().verifyLogin();
      }
    
      public void verifyLogin()
      {
        try {
          File f = new File("UserDetails.txt");
          Scanner fileRead = new Scanner(f);
    
          boolean matched = false;
          while(fileRead.hasNextLine())
          {
            String textLine = fileRead.nextLine();
            String[] userDetails = textLine.split(" : ");
            String tempUsername = userDetails[3];
            String tempPassword = userDetails[4];
    
            if(tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim()))
            {
              matched = true;
              break;
            }
          }
    
          if (matched)
          {
            //new LibraryCatalogFrame().setVisible(true);
            System.out.println("Show JFrame");
          }
          else
          {
            System.out.println("Show dialog");
            //JOptionPanes.messageBox("Please re-enter your user details", "Incorrect Username or Password");
          }
        }
        catch (FileNotFoundException e)
        {
          e.printStackTrace();
          //JOptionPanes.messageBox("Error", "FileNotFound");
        }
      }
    }