Results 1 to 5 of 5
Thread: Password Checker
- 02-17-2013, 08:40 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Password Checker
I have a problem with this code and can't seem to find what it is. It compiles correctly but doesn't work as it should ie ( verifies password meets all requirements) it just says "valid" no matter what. Can someone help me? Your help is appreciated.
import java.util.Scanner;
public class PasswordChecker
{//start class
public static void main(String[] args)
{//start main
/************************************************** ********
* Declare and initialize variables
************************************************** ********/
Scanner stdIn = new Scanner(System.in);
boolean containsDigit = true;
boolean containsLowerCaseLetter = true;
boolean containsUpperCaseLetter = true;
boolean containsSpecialCharacter = true;
boolean noAnd = true;
boolean noEnd = true;
boolean length = true;
String password;
/************************************************** *********************
*Displays message for user input and loops
************************************************** **********************/
int numberOfTries = 0;
while (numberOfTries < 3)
{// While loop to Continue to prompt the user for a password until these criteria are met.
System.out.println("Password Verifier");
System.out.println("Please enter a password that meets the following rules: ");
System.out.println("Is at least 8 characters long ");
System.out.println("Contains at least 1 lower letter character ");
System.out.println("Contains at least 1 upper letter character ");
System.out.println("Contains at least 1 numeric digit ");
System.out.println("Contains at least 1 special character from the set: !@#$%^&* ");
System.out.println("Does not contain the word \"and\" or the word \"end\"");
password = stdIn.next();//input for password
numberOfTries++;
/************************************************** ********
*Decision statements for password requirements
************************************************** ********/
if (password.length() >= 8)
{// If length greater than or equal to 8 characters in length its a good password
length = true;
}
for (int i = 0; i < password.length(); i++)
{
char ch = password.charAt(i);
if (Character.isDigit(ch))
{// Check for Digits in password
//• Contains at least 1 numeric digit
containsDigit = true;
}
if (Character.isLetter(ch) && Character.isLowerCase(ch) ) {// Check for Letters in password
//• Contains at least 1 lower letter character
containsLowerCaseLetter = true;
}
if (Character.isLetter(ch) && Character.isUpperCase(ch) ) {// Check for Letters in password
//• Contains at least 1 upper letter character
containsUpperCaseLetter = true;
}
if(ch == '!' || ch == '@' || ch == '#' || ch == '$' || ch == '%' || ch == '^' || ch == '&' || ch == '*'){
//• Contains at least 1 special character from the set: !@#$%^&*
containsSpecialCharacter = true;
}
if(password.indexOf("and") == -1){
//• Does not contain the String “and”
noAnd = true;
}
if(password.contains("end")){
//• Does not contain the String “end”
noEnd = true;
}
}
//if conditions are true print valid
if (true)
{
System.out.println("Valid");
}
// if conditions are false print invalid
else if(false)
{
System.out.println("Invalid");
}
else if(!length)
{
//does not meet length requirements
System.out.println("Invalid" + "\n" + "Must contain at least 8 characters");
}
else if(!containsLowerCaseLetter)
{
//Does not contain a lowercase letter
System.out.println("Invalid" + "\n" + "Must contain at least 8 characters");
}
else if(!containsUpperCaseLetter)
{
//Does not contain an uppercase letter
System.out.println("Invalid" + "\n" + "Must contain at least one uppercase character");
}
else if(!containsDigit)
{
//Does not contain a digit
System.out.println("Invalid" + "\n" + "Must contain at least one digit");
}
else if(!containsSpecialCharacter)
{
//Does not contain a special character
System.out.println("Invalid" + "\n" + "Missing a special character");
}
else if(!password.contains("and"))
{
//Does not contain the String “and”
System.out.println("Invalid" + "\n" + "Contains the string \"and\"");
}
else if(!password.contains("end"))
{
//Does not contain the String “end”
System.out.println("Invalid" + "\n" + "Contains the string \"end\"");
}
}
}
}
- 02-17-2013, 09:03 PM #2
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 902
- Rep Power
- 1
Re: Password Checker
Here is the first part of your if-else test area.
The code will always print valid because the first condition is always true and the next ones are never even considered.Java Code://if conditions are true print valid if (true) { System.out.println("Valid"); } // if conditions are false print invalid else if(false) { System.out.println("Invalid"); }
Jim
- 02-17-2013, 09:22 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
Re: Password Checker
Thanks for your response. How do I get it to check that each variable is true then? Also, is there an easier way to print the statement "invalid" + the corresponding statement(s) that make it invalid.
- 02-17-2013, 10:18 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 902
- Rep Power
- 1
Re: Password Checker
First you should initialize all your booleans to false (assume the password is bad and set the flags to true if they check out).
Then to see if the entire password is either valid or invalid, just 'AND' all the booleans together. If only one is false, the result will be false.
Then you can do a separate test so show where the password was badJava Code:boolean isValid = containsLowerCaseLetter && containsUpperCaseLetter && containsDigit .....
To facilitate printing out the specific error messages, you could do something like this;Java Code:if (!isValid) { System.out.println("Password is not valid\n"); // code to show specifics } else { System.out.println("Password is valid\n"); }
There are certainly more clever and optimal ways of doing it. I haven't tested this but you should get the idea. I may have overlooked something.Java Code:String ErrorMsg = containDigits ? "" : "Password did not contain digits\n"; ErrorMsg = containsUpperCase ? ErrorMsg : ErrorMsg + "No upper case characters\n"; ErrorMsg = containsLowerCase ? ErrorMsg : ErrorMsg + "No lower case characters\n"; // rest of tests here System.out.println(ErrorMsg);
Regards,
JimLast edited by jim829; 02-17-2013 at 10:33 PM. Reason: Forgot to finish answering question
The Java™ Tutorial
YAT -- Yet Another Typo
- 02-17-2013, 10:47 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- United States
- Posts
- 902
- Rep Power
- 1
Re: Password Checker
One more thing I forgot to mention. Since your tests for digits or characters return boolean values, you don't need if statements.
For example, lets say some method returns true or false if the character is a digit or not:
The previous is actually sayingJava Code:boolean digitfound = false; // booleans default to false anyway but this is clearer if (isDigit(ch)) digitfound = true;
So just use the return value of your method as the actual assignment value;Java Code:if (isDigit(ch) == true) { digitfound = true; }
digitfound will then be either true or false depending on whether ch was a digit or not.Java Code:digitfound = isDigit(ch);
Regards,
JimThe Java™ Tutorial
YAT -- Yet Another Typo
Similar Threads
-
Sudoku checker
By kennemercollege in forum New To JavaReplies: 3Last Post: 04-27-2011, 12:41 PM -
isbn checker
By jingliu316 in forum New To JavaReplies: 2Last Post: 10-27-2010, 07:31 PM -
how to check password for 3 times enterd wrong password
By sk.mahaboobbhasha@gmail.c in forum New To JavaReplies: 2Last Post: 11-14-2008, 07:53 PM -
how to check password for 3 times enterd wrong password
By sk.mahaboobbhasha@gmail.c in forum Java ServletReplies: 0Last Post: 11-14-2008, 01:22 PM -
How to check password of a jsp/html with the password of Database(mysql) #1
By sk.mahaboobbhasha@gmail.c in forum Java ServletReplies: 2Last Post: 11-14-2008, 01:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks