Need help with program that checks requirements
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.
Code :
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;
}
Re: Need help with program that checks requirements
im not sure but try to use pater
for enter password use
Code :
boolean b=true;
while(b){ ...
if (pass.leght>6; b=false;)
}
now make 3 patern
Pattern p = Pattern.compile(myMagicPattern); // insert your pattern here
Matcher m = p.matcher(contentString);
if (m.find()) 3 times pass is ok
patern for lower characters [a-z] , upper [A-Z] , digits [0-9] ; lol 1 pattern [a-zA-Z0-9]
just try it
Re: Need help with program that checks requirements
I think I fixed it.
I don't know what I was doing with all those returns in the first code. I cleaned it up a bit, and it seems to work!
Also, thank you Bojan for the suggestion. However, I needed to do it this way.
Code Java:
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 (len = 0; len < testPW.length(); len++)
{
if (Character.isUpperCase(testPW.charAt(len)))
{
countU++;
}
else
if (Character.isLowerCase(testPW.charAt(len)))
{
countL++;
}
else
if (Character.isDigit(testPW.charAt(len)))
{
countD++;
}
}
if ((countU > 0) && (countL > 0) && (countD > 0))
{
PassOK = true;
}
else
PassOK = false;
}
else PassOK = false;
return PassOK;
}
Re: Need help with program that checks requirements
Actually, you were putting your else if(UC==0) return something.
This will definitely behave very different as your count is initialized to 0 and whenever it'll not find first upper case it'll throw your code out.
So, instead of writing else there, put it outside the loop and check out there as you have done in your last post. Anyways, good luck and i read this late.
Re: Need help with program that checks requirements
Thanks Mr. 777, I get what you're saying!