因此,我必须编写一个程序,提示用户输入密码,该密码必须遵循三个要求:至少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;
}
}
}我很抱歉,如果我的一些问题似乎是初级的,任何帮助都将不胜感激,谢谢!
发布于 2016-07-22 07:58:11
如何才能将该密码作为字符串接受
checkPassword(new String(input.getPassword))或更新您的方法以接受char[]而不是字符串。
至于错误检查,你应该使用throw new ShortPasswordException(),在你实现了一个像ShortPasswordException extends Exception这样的类之后,你想要抛出错误的地方。
然后,您可以这样做
try {
checkPassword();
} catch (ShortPasswordException e) {
// TODO: Handle exception
}更具冒险精神的提示:使用正则表达式检查密码要求。例如,匹配\d{2}就意味着你有两个连续的数字。
发布于 2016-07-22 08:03:00
马上就有两件事:
(1)确保你导入的是正确的类,不要依赖IDE来做正确的导入。您正在从JavaFX导入ActionEvent类,但是您正在使用的框架是Swing。
变化
import javafx.event.ActionEvent;至
import java.awt.event.ActionEvent;我不太熟悉JavaFX和Swing如何很好地相互配合,但使用正确的类通常有助于避免令人头疼的问题和编译/运行时错误。
(2) java.lang.String类中的静态方法提供了一种将字符数组转换为字符串的便捷方法。在您的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);
}
}https://stackoverflow.com/questions/38516048
复制相似问题