1

因此,我必须编写一个提示用户输入密码的程序,该密码必须遵循三个要求:至少 8 个字符长,只有字母和数字,以及至少两位数字。现在,我创建的用于检查这三个要求的方法我认为是合理的,但是我的程序还必须进行一些异常处理,并且如果其中一个要求关闭,则能够要求用户重新输入密码并显示相应的错误消息遵循每个要求。我创建了一个字符串 errorMessage 来中继该消息,但是当我尝试在我的 main 中调用它时它给了我一个错误?

我的另一个问题是必须使用 JPasswordField 将密码输入我的程序,但我什至很难设置它,因为我阅读的 JPanel、按钮和动作事件等许多其他因素必须随之而来. 我尝试使用 JPasswordField 并注意到输入密码的行将其作为数组输入,当我的 checkPassword 方法需要一个字符串时,我怎样才能将该密码作为字符串输入?

到目前为止,这就是我的程序所拥有的:

   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() < 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 < x.length(); i++) {
      if (!Character.isLetter(x.charAt(i)) && !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 < x.length(); i++) {
      if (Character.isDigit(x.charAt(i))){
        count++;
      }
    }

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

如果我的一些问题看起来很初级,我提前道歉,任何帮助将不胜感激,谢谢!

4

2 回答 2

1

两件事情马上开始:

(1) 确保您正在导入正确的类,不要依赖 IDE 进行正确的导入。您正在从 JavaFX 导入 ActionEvent 类,但您使用的框架是 Swing。

改变

import javafx.event.ActionEvent;

import java.awt.event.ActionEvent;

我对 JavaFX 和 Swing 相互配合的效果不是很熟悉,但使用正确的类通常有助于避免头痛和编译/运行时错误。

(2) 类中的静态方法java.lang.String提供了将 char 数组转换为字符串的便捷方法。在您的actionPerformed方法中,添加以下内容:

String passStr = String.valueOf(password);

例如

@Override
public void actionPerformed(ActionEvent e) {
    // Get the source of the event, we know it is a JPasswordField so we cast it.
    JPasswordField input = (JPasswordField) e.getSource();
    // Get the char array from the input the user typed stored in the input object.
    char[] password = input.getPassword();
    // Convert char[] to String
    String passwordStr = String.valueOf(password);
    if(checkPassword(passwordStr)){
        JOptionPane.showMessageDialog(frame, "Congradulations, your password follows all the requirements");
    } else {
        JOptionPane.showMessageDialog(frame, errorMessage);
    }
}
于 2016-07-22T00:02:59.527 回答
1

我怎样才能将该密码作为字符串输入

要么checkPassword(new String(input.getPassword))或更新您的方法以接受 achar[]而不是 String。

至于错误检查,你应该throw new ShortPasswordException()在你想抛出那个错误的地方使用,例如,在你实现一个类之后ShortPasswordException extends Exception

然后,你可以做

try { 
    checkPassword();
} catch (ShortPasswordException e) { 
    // TODO: Handle exception
}

更冒险的提示:使用正则表达式检查您的密码要求。\d{2}例如,匹配表示您有 2 个连续数字。

于 2016-07-21T23:58:10.603 回答