这是我的工作,现在的任务...密码验证
实施一项计划,它读取用户密码并验证它符合以下标准: 是ATLEAST 8个字符长
包含ATLEAST 1个下部字母字符
包含ATLEAST 1上字母字符
包含ATLEAST 1数字位
包含从集合,包括至少一个特殊字符:!@#$%^ & *
不包含“和”字或单词“结束”
输出提示用户输入一个字符串并在输出中包含上述要求 。
输出一个表示有效或无效的字符串。如果无效,说明上面的规则没有得到满足。
利用了以下功能:
的indexOf
循环结构
的charAt()
ISDIGIT()
isUpperCase()
isLowerCase()
和所需的任何附加功能
对我来说,棘手的部分是它必须返回所有缺少的东西。就像我输入了一个表示密码的密码,它应该会回来告诉我“你错过了一个大写字母,一个数字和一个特殊字符”
我有一个开始,但我很困惑如何让它回报给我。 这是我迄今为止
/********************************************
This program will test a password for:
8 characters
1 upper case
1 lower case
1 numeric digit
1 special character from the set [email protected]#$%^&*
and make sure it doesn't contain AND or END
If the password complies, it will return a valid answer
If not, it will tell the user what they need to do.
*********************************************/
import java.util.Scanner;
public class YoungAmyProg5
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String input;
//input password
System.out.println("Enter a password that follows these rules:\n Is at least 8 characters long\n Contains at least 1 lower case letter\n Contains at least 1 upper case letter\n Contains at least 1 numeric digit\n Contains at least 1 special character from the set: ! @#$%^&*\n Does NOT contain the word "and" or the word "end": ")
input= in.nextLine();
//Put through string and reply
if
public static boolean isSecurePassword(String password) {
int lengthPassword = password.length();
if (lengthPassword >= 8) {
return false;
}
boolean hasUppercase = false; //uppercase
boolean hasLowecase = false; //lowercase
boolean hasDigit = false; //digit
int specialChar = input.indexOf('!', '@', '#', '$', '%', '^', '&', '*'); //special character
int word = input.indexOf ('and', 'end'); //and or end
for (int i = 0; i < lengthPassword; i++) {
char ch = password.charAt(i);
if (Character.isUpperCase(ch)) {
hasUppercase = true;
}
if (Character.isLowerCase(ch)) {
hasLowercase = true;
}
if (Character.isDigit(ch)) {
hasDigit = true;
}
if (specialChar>0) {
specialChar = true;
}
if (word>0) {
word = true;
}
你应该去'regex'。这将使你的任务变得简单 – frunkad 2015-02-11 19:11:07
为什么不只是让你的布尔标志为全局的,如果你需要它们在你的函数之外呢? – ChrisStillwell 2015-02-11 19:11:45
“我真的很困惑如何让它回报给我。”你想回报什么东西?另外,为什么在那里有一个神奇的浮动'if'? – leigero 2015-02-11 19:15:03