我正在尝试创建一个程序,将用户的输入作为密码进行比较,以查看密码是否符合要求。如果不符合要求,请再次提示用户输入密码,直至满足要求为止。这就是我所拥有的,我不明白为什么它不起作用......
import javax.swing.*;
public class Password {
public static void main(String[] args) {
//
String pInput = "";
do {
pInput = JOptionPane.showInputDialog(null, "Please enter your password.\n"
+ "Your password must have 6-10 characters\n"
+ "Your password must contain at least one letter and one digit");
}
while (authenticate(pInput) == false);
JOptionPane.showMessageDialog(null, "Your password was successfully entered.");
}
private static boolean authenticate(String password)
{
// The password should be at least six characters long.
// The password should contain at least one letter.
// The password should have at least one digit.
if ((password.length() > 6) &&
(password.length() < 10) &&
(password.matches("[a-z]")) &&
(password.matches("[0-9]")))
return true;
else
return false;
}
}
答案 0 :(得分:3)
如果我理解了您的问题,我会重命名您的authenticate()
方法(它真的是validate()
),
// validate that a password adheres to the "rules".
private static boolean validate(String password) {
// Check for null, then a length less then 6 (and I really don't like the length()
// > 10 check, that's a BAD requirement).
if (password == null || password.length() < 6 || password.length() > 10) {
return false;
}
boolean containsChar = false;
boolean containsDigit = false;
for (char c : password.toCharArray()) {
if (Character.isLetter(c)) {
containsChar = true;
} else if (Character.isDigit(c)) {
containsDigit = true;
}
if (containsChar && containsDigit) {
return true;
}
}
return false;
}
就个人而言,我宁愿避免使用正则表达式,因为它们经常令人困惑。如果这是一项要求,则可以使用@MadProgrammer作为对问题的评论添加的那个,
private static final Pattern pattern = Pattern
.compile("^(?=.*\\d)(?=.*[a-zA-Z]).{6,10}$"); // <-- note "\\d"
private static boolean validate(String password) {
return pattern.matcher(password).matches();
}
答案 1 :(得分:2)
你应该改变:
password.length()&gt; 6进入password.length()&gt; = 6
password.length()&lt; 10到password.length()&lt; = 10
因为你想要至少六个字符长。和大多数十个字符。
password.matches(&#34; [az]&#34;)到password.matches(&#34;。 [az] +。&#34;)因为.matches(&#34; [az]&#34;)用于检查不是字符串的字符
这个是编辑版
private static boolean authenticate(String password)
{
// The password should be at least six characters long.
// The password should contain at least one letter.
// The password should have at least one digit.
if ((password.length() >= 6) &&
(password.length() <= 10)&&
(password.matches(".*[a-z]+.*")) &&
(password.matches(".*[0-9]+.*")) )
return true;
else
return false;
}
}
答案 2 :(得分:1)
更改身份验证方法中的正则表达式:
private static boolean authenticate(String password)
{
// The password should be at least six characters long.
// The password should contain at least one letter.
// The password should have at least one digit.
if ((password.length() >= 6) &&
(password.length() <= 10) &&
(password.matches("^(?:.*[a-z].*)(?:.*[0-9].*)$")))
return true;
else
return false;
}
使用non-capturing超前模式将启用您想要的验证。 原始版本不起作用,因为它试图将密码与“[a-z]”和“[0-9]”同时匹配 - 这个条件总是返回false!
答案 3 :(得分:0)
您需要更改:
password.length() > 6
进入: password.length() >= 6
password.length() < 10
进入: password.length() <= 10
(password.matches("[a-z]")) && (password.matches("[0-9]"))
进入: password.matches(".*[a-z]+.*") && password.matches(".*[0-9]+.*")
前两个是因为你希望6-10范围是包容性的(所以它听起来符合你的要求) 第三个变化是因为您需要常规模式来匹配整个密码,而不仅仅是单个字符。
答案 4 :(得分:0)
我认为问题在于函数的阻塞,使用以下代码
if ((password.length() >= 6) &&
(password.length() <= 10) &&
(password.matches(".*[a-z]+.*")) &&
(password.matches(".*[0-9]+.*")))
return true;