简体   繁体   中英

Java password checker with strings

Been fighting with this for awhile, still new to java and really struggling with this assignment. We're meant to make a password checker assignment that checks if 2 entered passwords are the same. Then also check them against some "requirements" such as password length of at least 8 characters, one special character, one upper and one lower case, no repeating characters more than 3 times consecutively, (ie "aaa").

Any input or criticism is very much appreciated and thank you in advance.

import java.util.Scanner;

/**
 *
 * @author Jonot
 */
public class Passwords {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //Asks and records users inputted passwords
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter your password");
        String password = input.nextLine();
        System.out.println("Please re-enter your password");
        String passwordCheck = input.nextLine();
        //boolean to check if the inputted passwords meets requirements set below
        boolean check;
        check = isConfirmed(password);
        while (!password.equals(passwordCheck) || (!check)) {
            System.out.println("The password you entered is invalid");
            System.out.println("Please try again");
            password = input.nextLine();
            System.out.println("please re-enter");
            passwordCheck = input.nextLine();

            check = isConfirmed(password);
            //String password = input.nextLine();
            //check = isConfirmed(password);

            //if passwords meets boolean requirements, 
            if (isConfirmed(password)) {
                //this will print out. Doesn't work right now.
                System.out.println("password is valid");
            } else {
            }

        }

    }
    //Boolean variables and they're set requirements. Do not work right now.
    //Not sure why.

    public static boolean isConfirmed(String password) {
        Boolean leastOneUpperCase = false;
        Boolean leastOneLowerCase = false;
        Boolean leastOneDigit = false;
        Boolean oneSpecialCharacter = false;

        if (password.length() < 8) {
            return false;
        }

        for (int i = 0; i < password.length(); i++) {
            if (Character.isUpperCase(password.charAt {i

                    ));

                    {
              leastOneUpperCase = true;
                }
                else if (Character.isLowerCase(password.charAt(i)));
                {
                    leastOneLowerCase = true;
                }
                else if (Character.isDigit(password.charAt(i)));
                {
                    leastOneDigit = true;
                }
            }
            return (leastOneUpperCase && leastOneLowerCase && leastOneDigit);

        }
        return false;
    }

There was one primary problem, in the isConfirmed method. The return statement with the variables in it was inside the loop that checked each character. So, the return statement was executed after the first character was checked and therefore always returned false, since two out of the three booleans would still be false.

Also, looked like some semicolons were out of place in your charAt checks. Not sure if that's a formatting error introduced during your post. Finally, I thought the code would be more readable by pulling the current character being checked into a variable and using that instead of calling charAt a bunch of times.

Here's the revised code:

import java.util.Scanner;

/**
 *
 * @author Jonot
 */
public class Passwords {

    /**
     * @param args the command line arguments
     */
    public static final void main(String[] args) {
        //Asks and records users inputted passwords
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter your password");
        String password = input.nextLine();
        System.out.println("Please re-enter your password");
        String passwordCheck = input.nextLine();
        //boolean to check if the inputted passwords meets requirements set below
        boolean check;
        check = isConfirmed(password);
        while (!password.equals(passwordCheck) || (!check)) {
            System.out.println("The password you entered is invalid");
            System.out.println("Please try again");
            password = input.nextLine();
            System.out.println("please re-enter");
            passwordCheck = input.nextLine();

            check = isConfirmed(password);
            //String password = input.nextLine();
            //check = isConfirmed(password);

            //if passwords meets boolean requirements, 
            if (isConfirmed(password)) {
                //this will print out. Doesn't work right now.
                System.out.println("password is valid");
            } else {
            }

        }

    }
    //Boolean variables and they're set requirements. Do not work right now.
    //Not sure why.

    public static boolean isConfirmed(String password) {
        Boolean leastOneUpperCase = false;
        Boolean leastOneLowerCase = false;
        Boolean leastOneDigit = false;
        Boolean oneSpecialCharacter = false;

        if (password.length() < 8) {
            return false;
        }

        for (int i = 0; i < password.length(); i++) {
            char c = password.charAt(i);

            if (Character.isUpperCase(c))
            {
                leastOneUpperCase = true;
            }
            else if (Character.isLowerCase(c))
            {
                leastOneLowerCase = true;
            }
            else if (Character.isDigit(c))
            {
                leastOneDigit = true;
            }
                System.out.println("password is valid");
            } else {
            }

        }

    }
    //Boolean variables and they're set requirements. Do not work right now.
    //Not sure why.

    public static boolean isConfirmed(String password) {
        Boolean leastOneUpperCase = false;
        Boolean leastOneLowerCase = false;
        Boolean leastOneDigit = false;
        Boolean oneSpecialCharacter = false;

        if (password.length() < 8) {
            return false;
        }

        for (int i = 0; i < password.length(); i++) {
            char c = password.charAt(i);

            if (Character.isUpperCase(c))
            {
                leastOneUpperCase = true;
            }
            else if (Character.isLowerCase(c))
            {
                leastOneLowerCase = true;
            }
            else if (Character.isDigit(c))
            {
                leastOneDigit = true;
            }
        }

        return (leastOneUpperCase && leastOneLowerCase && leastOneDigit);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM