JAVA密码检查程序-检查密码是否有2位以上的数字+只有字母和数字



好吧,我正在制作一个密码检查器程序,其中有一些规则。密码必须为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.");
        }
    }
}

这里有一个解决方案。它不是有效的,但是它应该对初学者有帮助。它将问题分解为多个步骤。对密码的每一项要求都会一步一步地进行测试。如果在任何时候,违反了其中一个要求,则该方法返回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;
}

开始阅读并使用java中的模式regex。下面的链接有很好的解释,你可以找到很多例子。我希望这就是你想要解决的问题。http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

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

Regex是首选武器,使用它,解决方案可以简化为单个方法调用:

boolean correct = password.matches("(?=(.*\d){2})[a-zA-Z0-9]{8,}");
   public boolean isPassOk(String val)
{
   boolean isUp       = false;
   boolean isDown     = false;
   boolean isNum      = false;
   boolean isSPecial  = false;
   char c[] = val.toCharArray();
   for(int k = 0; k<c.length; k++)
   {
       int cc = (int)c[k];
       if(cc >= 48 && cc <= 57 )  {isNum = true;} else
       if(cc >= 65 && cc <= 90 )  {isUp = true;} else
       if(cc >= 97 && cc <= 122 ) {isDown = true;} else
           isSPecial = true;

   }
   if(isDown && isUp && isNum && isSPecial) return true;
   return false;
}

相关内容

最新更新