The problem that I am having is that I can\'t get my Password Verification Program to check a string to ensure that, 1 of the characters is in upper case and one is in lower
A loop like this one:
else if (!(Character.isLowerCase(ch)))
{
for (int i=1; i
Has an obvious logical flaw: You enter it if the first character is not lowercase, then test if the second character is not lower case. At that point you throw an error.
Instead, you should do something like this (not full code, just an example):
boolean hasLower = false, hasUpper = false, hasNumber = false, hasSpecial = false; // etc - all the rules
for ( ii = 0; ii < password.length(); ii++ ) {
ch = password.charAt(ii);
// check each rule in turn, with code like this:
if Character.isLowerCase(ch) hasLower = true;
if Character.isUpperCase(ch) hasUpper = true;
// ... etc for all the tests you want to do
}
if(hasLower && hasUpper && ...) {
// password is good
}
else {
// password is bad
}
Of course the code snippet you provided, besides the faulty logic, did not have code to test for the other conditions that your "help" option printed out. As was pointed out in one of the other answers, you could consider using regular expressions to help you speed up the process of finding each of these things. For example,
hasNumber : use regex pattern "\d+" for "find at least one digit"
hasSpecial : use regex pattern "[!@#$%^&*]+" for "find at least one of these characters"
In code:
hasNumber = password.matches(".*\\d.*"); // "a digit with anything before or after"
hasSpecial = password.matches(".*[!@#$%^&*].*");
hasNoNOT = !password.matches(".*NOT.*");
hasNoAND = !password.matches(".*AND.*");
It is possible to combine these things in clever ways - but especially when you are a novice regex user, it is much better to be a little bit "slow and tedious", and get code that works first time (plus you will be able to figure out what you did six months from now).