Java JPasswordField
<p>So I have to write a program that prompts the user to enter a password that has to follow three requirements: at least 8 characters long, only letters and digits, and at least two digits. Now the method I created to check these three requirements I believe is sound, but my program also has to do some exception handling and be able to ask the user to reenter the password if one of the requirements is off and display the respective error message to go along with each requirement. I created a string errorMessage to relay that message but it gives me an error when i try to call it in my main ? </p>

<p>My other issue is that the password must be taken in to my program by using JPasswordField but I am struggling with even setting it up because of the numerous other factors like the JPanel, buttons, and action events that I read has to go along with it. I attempted to use JPasswordField and noticed that the line that takes in the password, takes it in as an array, when my checkPassword method needs a string, how can i take in that password as a string instead? </p>

<p>This is what I have for my program so far:</p>

<pre><code>   import java.awt.event.ActionListener;
   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JOptionPane;
   import javax.swing.JPasswordField;

   import javafx.event.ActionEvent;

public class Ed10Chp6Ex6Point18CheckPasswordProgram {

public static void main(String[] args) {

    final JFrame frame = new JFrame("Check Password Program");
    JLabel jlbPassword = new JLabel("Please enter the password: ");
    JPasswordField jpwName = new JPasswordField(15);
    jpwName.setEchoChar('*');
    jpwName.addActionListener(new ActionListener()) {

        public void actionPerformed(ActionEvent e) {
            JPasswordField input = (JPasswordField) e.getSource();
            char[] password = input.getPassword();

            if(checkPassword(password)){
                JOptionPane.showMessageDialog(frame, "Congradulations, your password follows all the requirements");
            }
            else{
                JOptionPane.showMessageDialog(frame, errorMessage);
            }
        }
    }
}

public static boolean checkPassword(String x) {

    String errorMessage = "";

    //must have at least eight characters
    if (x.length() &lt; 8){
      errorMessage = "The password you entered is invalid, password must be at least 8 characters";
        return false;
    }

    //consists of only letters and digits
    for (int i = 0; i &lt; x.length(); i++) {
      if (!Character.isLetter(x.charAt(i)) &amp;&amp; !Character.isDigit(x.charAt(i))) {
          errorMessage = "The password you entered is invalid, password must contain only letters and digits";
        return false;
      }
    }

    //must contain at least two digits
    int count = 0;
    for (int i = 0; i &lt; x.length(); i++) {
      if (Character.isDigit(x.charAt(i))){
        count++;
      }
    }

    if (count &gt;= 2){
      return true;
    }
    else {
        errorMessage = "The password you entered is invalid, password must contain at least two digits";
      return false;
    }
}
}
</code></pre>

<p>I apologize in advanced in case some of my questions seem rudimentary, any help would be greatly appreciated, thank you!              </p>