简体   繁体   English

带字符串的Java密码检查器

[英]Java password checker with strings

Been fighting with this for awhile, still new to java and really struggling with this assignment. 一直为此奋斗了一段时间,对于Java还是一个新手,并且确实在为此工作上苦苦挣扎。 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"). 然后还要检查它们是否符合某些“要求”,例如密码长度至少为8个字符,一个特殊字符,一个大写字母和一个小写字母,并且连续三个重复的字符都不超过连续3次(即“ 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. isConfirmed方法中存在一个主要问题。 The return statement with the variables in it was inside the loop that checked each character. 包含变量的return语句位于检查每个字符的循环内。 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. 因此, return语句是在检查第一个字符之后执行的,因此总是返回false,因为三个布尔中的两个仍然是false。

Also, looked like some semicolons were out of place in your charAt checks. 而且,看起来在您的charAt检查中有些分号charAt 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. 最后,我认为通过将当前检查的字符放入变量中并使用它而不是多次调用charAt ,可以使代码更具可读性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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