Java密码搜索字符

时间:2018-04-03 04:47:20

标签: java

我必须创建一个创建有效密码的程序,此密码的要求是:必须包含5到8个字符,必须包含一个数字,一个大写字母和一个小写字母。它也必须是一个类和一个测试程序的形式,但现在我只是想让程序正常工作。就像现在一样,长度检查,大写检查工作正常,它的数字检查给我带来了问题。它说每次我输入一个数字密码都是无效的。这是迄今为止我所拥有的。

import java.util.Scanner;

public class pass {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String password;
        int length;
        boolean hasUppercase;
        boolean hasLowercase;
        boolean hasNum;

        do {
            System.out.println("Password must be between 5 and 8 characters 
            and must contain a lowercase letter, an uppercase letter, and a 
            number");
            System.out.println("Please enter your password: ");
            password = sc.next();
            length = password.length();
            hasUppercase = !password.equals(password.toLowerCase());
            hasLowercase = !password.equals(password.toUpperCase());
            hasNum = !password.matches(".*\\d+.*");


            if(length >= 5 & length <= 8 ) {
                if(hasUppercase & hasLowercase & hasNum) {
                    System.out.print("Your password has been set");

                }
            }
            else {
                System.out.print("Password must be between 5 and 8 
                characters and must contain a lowercase letter, an uppercase 
                letter, and a number");
            }

        }
        while(length <= 5 | length >= 8 | !hasUppercase | !hasLowercase | 
!hasNum);
    }
}

3 个答案:

答案 0 :(得分:1)

hasNum = !password.matches(".*\\d+.*");说&#34; hasNum = true如果字符串不包含数字&#34; - 只需删除!

我还简化了最终条件 - 一个布尔值&#34; passwordOk&#34;而不是再次检查所有5个条件。

答案 1 :(得分:0)

你可以让你的生活更轻松,只需使用正则表达式:

String password = "Hello123";
if (password.matches("(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z]).{5,8}")) {
    System.out.println("Password is valid");
}
else {
    System.out.println("Password is invalid.");
}

Demo

答案 2 :(得分:0)

编辑了你的程序。内联记录的变更

public class pass {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String password;
    int length;
    boolean hasUppercase;
    boolean hasLowercase;
    boolean hasNum;

    do {
        System.out.println("Password must be between 5 and 8 characters and must contain a lowercase letter, an uppercase letter, and a number");
        System.out.println("Please enter your password: ");
        password = sc.next();
        length = password.length();
        hasUppercase = !password.equals(password.toLowerCase());
        hasLowercase = !password.equals(password.toUpperCase());
        hasNum = !password.matches(".*\\\\d+.*");

        if(length >= 5 & length <= 8 ) {
            if(hasUppercase & hasLowercase & hasNum) {
                System.out.print("Your password has been set");

            }
        }
        else {
            System.out.print("Password must be between 5 and 8  characters and must contain a lowercase letter, an uppercase letter, and a number");
        }

    }
    while(length < 5 | length > 8 | !hasUppercase | !hasLowercase | !hasNum); //Removed >= and <= for length checks
}

}