dimanche 30 octobre 2016

Java password program

Objective of program:

Program that requires the user to enter two passwords that are the same.

  1. The password must be at least 8 characters long.

  2. The password must contain at least:

    • ONE alpha character
    • ONE numeric character [0-9]
    • ONE character that is not alpha or numeric, such as: ! @ $ % ^ & * ( ) - _ = + [ ] ; : ' " , < . > / ?
  3. The password MUST NOT:

    • contain spaces;
    • begin with an exclamation [!] or a question mark [?];
    • contain repeating character strings of 3 or more identical characters, such as “1111” or “aaa”.

My code:

public class PasswordChecking { // start of class 

    public static void main(String[] args) { // start of main method

        String passWord1; // the first password the user will enter
        String passWord2; // the second password the user will enter


        // scanner method declared to get user input
        Scanner kb = new Scanner(System.in); 


        // asks the user to print out the first password
        System.out.print("Please enter a password: \n");
        passWord1 = kb.nextLine();

        // asks the user to print out the second password
        System.out.print("\nRe-enter the password: \n");
        passWord2 = kb.nextLine();

        // method call for the length of the character
        characLength(passWord1, passWord2);

        // method call for the characters that should be in the passwords
        if(!passContain(passWord1, passWord2)){
            System.out.println("\nYour password must contain at least: "
                       + "\n- one alpha character, "
                       + "\n- one numeric number, and "
                       + "\n- one character that is neither numeric nor alphabetical.\n");

            // if condition in the if statement is true, when the... 
            // program reaches this statement, the program is terminate 
            System.exit(0); 
        } // end of if statement

        // method call for what the password cannot contain
        if(!notContain(passWord1)){
            System.out.println("\nYour password must not contain:"
                       + "\n- spaces, "
                       + "\n- begin with '!' or '?', and "
                       + "\n- cannot contain repeating character of 3 or more "
                       + "identical characters.");

            // if condition in the if statement is true, when the... 
            // program reaches this statement, the program will terminate 
            System.exit(0); 
        } // end of if statement


        if(passWord1.equals(passWord2)){ 
            System.out.println("\nYour passwords are equal.");
        } // end of if statement 
        else{ // else statement
            System.out.println("\nYour passwords are not equal.\nPlease create two "
                                + "passwords that are equal.");
        } // end of else

    } // end of main()


    public static void characLength(String pass1, String pass2){ 

        // if statement to obtain at least 8 characters for the passwords
        if(pass1.length() < 8 && pass2.length() < 8){ 
            System.out.println("\nYour passwords must be at least "
                                + "8 characters long.");

            System.exit(0); 
        } // end of if statement 

    } // end of characLength()


    public static boolean passContain(String pass1, String pass2){
        boolean hasLetter = false; // if password has a letter - set to false
        boolean hasDigit = false; // if password has a digit - set to false
        boolean hasNotAB = false; // if password has a special character - set to false

        // enhanced for loop for the password to check if it has a letter, ... 
        // a digit and a special character
        for(char a: pass1.toCharArray()){
            // start of if statement
            if(Character.isLetter(a)){
                hasLetter = true;
            } // end of if statement 
            else if(Character.isDigit(a)){ // check if password has at leat 1 digit
                hasDigit = true;
            } // end of else if
            else { // else statement
                hasNotAB = true;
            } // end of else statement
        } // end of enhanced for loop

        return hasLetter && hasDigit && hasNotAB;

    } // end of passContain()


    public static boolean notContain(String pass){
        boolean hasSpace = false; // if password has a space - set to false
        boolean hasRepeating = false;// if password has 3 or more repeating characters - set to false
        int prevchar = -1; // the previous password dclared and set to -1
        int repeating = 1; // if password is repeating 

        if(pass.charAt(0) == '!' || pass.charAt(0) == '?') {
            return false;
        } // end of if statement

        // enhanced for loop to test different conditions to...
        // satisfy the requirments for an appropriate password
        for(char a: pass.toCharArray()){
            // tests if the password has a space
            if(Character.isSpaceChar(a)){
                hasSpace = true;

            // is the current character == the previous char?
            if(a == prevchar){
                // repeating is incremented to see if the passwords contain...
                // the same character 3 or more times in a row
                repeating++; 
                // does the password contain the same character 3 or more times in a row?
                if (repeating >= 3) {
                    hasRepeating = true;
                } // end of if statement
            } // end of if statement for previous character == current character
            else { // else statement
                repeating = 1;
                prevchar = a;
            } // end of else statement
        } // end of loop

        } 

        return !hasSpace && !hasRepeating;

    } // end of notContain()

} // end of PasswordChecking class

My problem:

The program allows me to enter three or more identical characters, when it shouldn't.

Aucun commentaire:

Enregistrer un commentaire