带字符串的Java密码检查器

时间:2017-11-17 01:08:50

标签: java passwords charat

已经和它争吵了一段时间,仍然是java的新手并且真的在努力完成这项任务。我们打算进行密码检查程序分配,检查2个输入的密码是否相同。然后还要根据一些“要求”检查它们,例如密码长度至少为8个字符,一个特殊字符,一个上部和一个下部情况,连续重复字符不超过3次(即“aaa”)。

非常感谢任何意见和批评,并提前感谢您。

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;
    }

1 个答案:

答案 0 :(得分:0)

isConfirmed方法中存在一个主要问题。带有变量的return语句位于检查每个字符的循环内。因此,return语句在第一个字符被检查后执行,因此总是返回false,因为三个布尔值中的两个仍然是假的。

另外,在您的charAt支票中看起来有些分号不合适。不确定这是否是您帖子中引入的格式错误。最后,我认为通过将当前正在检查的字符拉入变量并使用它而不是多次调用charAt,代码将更具可读性。

以下是修订后的代码:

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);
    }
}