【问题标题】:JAVA password check program - check that the password has 2+ digits + only letters and numbersJAVA密码检查程序-检查密码是否有2位以上+只有字母和数字
【发布时间】:2015-11-06 03:15:38
【问题描述】:

好的,所以我正在制作一个密码检查程序,并且有一些规则。 密码必须为 8 个或更多字符 密码必须包含 2 位或更多位 密码只能是字母和数字 这是我到目前为止所拥有的 如何检查 2 位数字以及仅检查字母和数字?谢谢

import java.util.Scanner;

public class checkPassword {

    public static boolean passwordLength(String password) {
        boolean correct = true;
        int digit = 0; 

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


    public static void main(String[] args) {
        // Nikki Kulyk


        //Declare the variables
        String password;

        Scanner input = new Scanner(System.in);

        //Welcome the user
        System.out.println("Welcome to the password checker!");

        //Ask the user for input
        System.out.println("Here are some rules for the password because we like to be complicated:\n");
        System.out.println("A password must contain at least eight characters.\n" +
                "A password consists of only letters and digits.\n" +
                "A password must contain at least two digits.\n");
        System.out.println("Enter the password: ");
        password = input.nextLine();

        boolean correct = passwordLength(password);


        if (correct) {
            System.out.println("Your password is valid.");
        }
        else {
            System.out.println("Your password is invalid.");
        }

    }
}

【问题讨论】:

  • 检查这个link你会发现一些验证方法。
  • 谢谢,但我正在寻找一个实际的解释,而不仅仅是一个代码,因为我是一个初学者程序员,我并不完全理解所有这些东西的含义。

标签: java


【解决方案1】:

这里有一个解决方案。它效率不高,但它应该对初学者有帮助。它将问题分解为步骤。每次对密码的每个要求进行测试。如果在任何时候违反了其中一个要求,则该方法返回 false;一旦我们确定密码无效,就没有必要再做更多检查了。

该方法首先检查长度(如上)。然后它使用一个 for 循环来计算字符串中的位数。接下来,它使用正则表达式来确保密码中只有字母数字字符。在测试字符串是否为字母数字时,请注意逻辑否定 (!) 运算符。如果字符串 只包含字母和数字,则返回 false。

包含注释以说明和阐明逻辑。祝你好运。

public static boolean passwordLength(String password) {
    /* Declare a boolean variable to hold the result of the method */
    boolean correct = true;

    /* Declare an int variable to hold the count of each digit */
    int digit = 0; 

    if (password.length() < 8) {
        /* The password is less than 8 characters, return false */
        return false;
    }

    /* Declare a char variable to hold each element of the String */
    char element;

    /* Check if the password has 2 or more digits */
    for(int index = 0; index < password.length(); index++ ){

        /* Check each char in the String */
        element = password.charAt( index );

        /* Check if it is a digit or not */
        if( Character.isDigit(element) ){
            /* It is a digit, so increment digit */
            digit++;
        } // End if block

    } // End for loop 

    /* Now check for the count of digits in the password */
    if( digit < 2 ){
        /* There are fewer than 2 digits in the password, return false */
        return false;
    }

    /* Use a regular expression (regex) to check for only letters and numbers */
    /* The regex will check for upper and lower case letters and digits */
    if( !password.matches("[a-zA-Z0-9]+") ){
        /* A non-alphanumeric character was found, return false */
        return false;
    }

    /* All checks at this point have passed, the password is valid */
    return correct;

}

【讨论】:

    【解决方案2】:

    开始阅读和使用java中的模式、正则表达式。下面的链接有很好的解释,你可以找到很多例子。我希望这是你正在寻找的解决问题的方法。 http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

    【讨论】:

    • 有效,但变得超级丑陋:stackoverflow.com/questions/5142103/regex-for-password-strength - 只需使用 for 循环检查条件。
    • @zapl 我的回答是不正确还是不恰当。如果是,请帮助我理解
    • 不,你的答案是好的 :) 创建正确的正则表达式很复杂。
    • 好的。感谢您的澄清。 zapl 打算学习概念
    【解决方案3】:

    我也打算推荐正则表达式,但作为一个新程序员,它们可能难以理解。一个好的开始方法是将密码转换为字符数组,然后使用Character.isLetterOrDigit(c) 并计算位数。

    【讨论】:

      【解决方案4】:

      正则表达式是首选的武器,解决方案可以简化为单个方法调用:

      boolean correct = password.matches("(?=(.*\\d){2})[a-zA-Z0-9]{8,}");
      

      【讨论】:

        猜你喜欢
        • 2011-06-20
        • 2022-11-24
        • 2016-03-22
        • 2016-03-02
        • 2016-01-15
        • 2017-05-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多