Hi there,
I need to write a program that uses a class to check requirements of a password. The password needs to be at least 6 characters in length, contain 1 uppercase, 1 lowercase, and 1 digit. I wrote something, and I swore it worked at one point in time but now it's not working and I don't know what the issue could be. Any advice would be appreciated. Thanks
My logic is that:
1) It checks if the minimum length is acceptable. If not, it automatically results in asking for password again.
2) Then it if you have an upper case, the count goes up. And if the countU > 0 then it is okay. If the countU = 0, it kicks back and makes you enter password again.
3) It will do this for lowercase, and digits as well. Making sure that you need at least 6 characters, 1 uppercase, 1 lower case, and 1 digit.
However, my program doesn't work. It accepts weird passwords and kicks back acceptable passwords.
For example:
It denies this password, even though it fits the requirements of at least 6 characters, 1 uppercase, 1 lowercase, and 1 digit.
Enter a Password:
sadfASD123
and then it accepts this password! Even though it doesn't fit the requirement.
Enter a Password:
ASDASDASDASD
So this makes me believe there is something very wrong with how I wrote the for loops.
public static boolean okPassword(String testPW) { int len = testPW.length(); boolean PassOK = true; int upC, countU = 0; int lowC, countL = 0; int digC, countD = 0; if (len >= 6) { for (upC = 0; upC < testPW.length(); upC++) { if (Character.isUpperCase(testPW.charAt(upC))) { countU++; PassOK = true; return PassOK; } else if (countU == 0) { PassOK = false; return PassOK; } } for(digC = 0; digC < testPW.length(); digC++) { if (Character.isDigit(testPW.charAt(digC))) { countD++; PassOK = true; return PassOK; } else if (countD == 0) { PassOK = false; return PassOK; } } for(lowC = 0; lowC < testPW.length(); lowC++) { if (Character.isLowerCase(testPW.charAt(lowC))) { countL++; PassOK = true; return PassOK; } else if (countL == 0) { PassOK = false; return PassOK; } } } else PassOK = false; return PassOK; }