Results 1 to 5 of 5
  1. #1
    lily_ruiz_06 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default 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\"");
    }
    }

    }

    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default Re: Password Checker

    Here is the first part of your if-else test area.

    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");
                }
    The code will always print valid because the first condition is always true and the next ones are never even considered.

    Jim

  3. #3
    lily_ruiz_06 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default 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.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default 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.

    Java Code:
    boolean isValid = containsLowerCaseLetter && containsUpperCaseLetter && containsDigit .....
    Then you can do a separate test so show where the password was bad
    Java Code:
    if (!isValid) {
    System.out.println("Password is not valid\n");
    // code to show specifics
    } else {
    System.out.println("Password is valid\n");
    }
    To facilitate printing out the specific error messages, you could do something like this;
    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);
    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.

    Regards,
    Jim
    Last edited by jim829; 02-17-2013 at 10:33 PM. Reason: Forgot to finish answering question
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,413
    Rep Power
    5

    Default 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:
    Java Code:
    boolean digitfound = false;  // booleans default to false anyway but this is clearer
    if (isDigit(ch))
      digitfound = true;
    The previous is actually saying
    Java Code:
    if (isDigit(ch) == true) {
      digitfound = true;
    }
    So just use the return value of your method as the actual assignment value;

    Java Code:
    digitfound = isDigit(ch);
    digitfound will then be either true or false depending on whether ch was a digit or not.


    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Sudoku checker
    By kennemercollege in forum New To Java
    Replies: 3
    Last Post: 04-27-2011, 12:41 PM
  2. isbn checker
    By jingliu316 in forum New To Java
    Replies: 2
    Last Post: 10-27-2010, 07:31 PM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 07:53 PM
  4. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 01:22 PM
  5. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 01:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •