Question: Java Programming. In this program, no matter what password the user types in, it spits out Invalid pattern for password detected and closes out the
Java Programming. In this program, no matter what password the user types in, it spits out "Invalid pattern for password detected" and closes out the program. Is there something wrong with the code? I have Login GUI.Java as m main first then Account.Java class in the next tab. Please assist. Also, if at all possible, please add comments in the program telling what exactly each thing does. It would be a HUGE help. Thanks again!
Account.java
import java.util.regex.Matcher; import java.util.regex.Pattern;
public class Account { private String userName, password; private static final String PASSWORD_PATTERN = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{9})"; public Account() { this.userName = ""; this.password = ""; } public Account(String userName, String password) { Pattern pattern = Pattern.compile(PASSWORD_PATTERN); Matcher matcher = pattern.matcher(password); if(!matcher.matches()) { System.out.println("Invalid pattern for password detected!"); System.exit(-1); } this.userName = userName; this.password = password; }
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getPassword() { return password; }
public void setPassword(String password) { Pattern pattern = Pattern.compile(PASSWORD_PATTERN); Matcher matcher = pattern.matcher(password); if(!matcher.matches()) { System.out.println("Invalid pattern for password detected!"); System.exit(-1); } this.password = password; } @Override public String toString() { return ("Username: " + this.userName + ", Password: " + this.password); } }
LoginAccountGUI.java (Main class)
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel;
public class LoginAccountGUI { private static JFrame mainFrame; private static JPanel mainPanel, buttonPanel; private static JButton createButton, loginButton, cancelButton; public static void main(String[] args) { Account account = new Account(); mainFrame = new JFrame("Account Creation and Login"); mainPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); createButton = new JButton("Create Account"); loginButton = new JButton("Login"); cancelButton = new JButton("Cancel"); buttonPanel.add(createButton); buttonPanel.add(loginButton); buttonPanel.add(cancelButton); mainPanel.add(buttonPanel); mainFrame.add(mainPanel); mainFrame.setSize(300, 100); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.setLocationRelativeTo(null); mainFrame.setVisible(true); // action listeners for the buttons createButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { String userName = JOptionPane.showInputDialog(null, "Please enter a username:").trim(); String password = JOptionPane.showInputDialog(null, "Please enter a password " + "Password must at least be 9 characters long and must contain at least: " + " -- one uppercase letter " + " -- one lowercase letter " + " -- one digit").trim(); account.setUserName(userName); account.setPassword(password); JOptionPane.showMessageDialog(null, "Account created successfully!"); }catch(NullPointerException npe){ JOptionPane.showMessageDialog(null, "All fields are mandatory. Please try again!"); System.exit(-1); } } }); loginButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(account.getUserName().equals("") || account.getPassword().equals("")) { JOptionPane.showMessageDialog(null, "Please create an account to login!"); return; } try { String originalUserName = account.getUserName().trim(); String originalPassword = account.getPassword().trim(); String userNameEntered = JOptionPane.showInputDialog(null, "Please enter the username:").trim(); String passwordEntered = JOptionPane.showInputDialog(null, "Please enter the password:").trim();
if(originalUserName.equals(userNameEntered) && originalPassword.equals(passwordEntered)) JOptionPane.showMessageDialog(null, "Congratulations! You logged in successfully."); else JOptionPane.showMessageDialog(null, "Sorry! Please check your username/password."); }catch(NullPointerException npe){ JOptionPane.showMessageDialog(null, "All fields are mandatory. Please try again!"); System.exit(-1); } } }); cancelButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
