Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过使用正则表达式在Java中输入密码_Java_Regex_Passwords - Fatal编程技术网

通过使用正则表达式在Java中输入密码

通过使用正则表达式在Java中输入密码,java,regex,passwords,Java,Regex,Passwords,我正在尝试使用正则表达式生成密码,但我不明白为什么我的代码不起作用: import java.util.Scanner; class test { public static void main (String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a password please."); String password = scann

我正在尝试使用正则表达式生成密码,但我不明白为什么我的代码不起作用:

import java.util.Scanner;

class test {

 public static void main (String[] args) {

    Scanner scanner = new Scanner(System.in);

    System.out.println("Enter a password please.");
        String password = scanner.nextLine();

    int x;
    String redex = "(^[0-9]+$)";
    String redexx = "(^[A-Z]+$)";

    boolean hasSpace = false;
    boolean hasUpperCase = false;
        boolean hasLetter = false;
        boolean hasDigit = false; // we set four booleans to be able to bring up individual error messages.

    if (password.length() > 8){
        hasLetter = true;
    } else {
        System.out.println("Your password needs to be at least 8 characters long.");                        

        if (password.matches(redexx)) { // we check to see if the password has at least an upper case letter, one or more digits and a space using regular expressions
            hasUpperCase = true;

        } if (password.matches(redexx)) { 
            hasDigit = true; 

        } if (password.indexOf(' ') == 1) {
            hasSpace = true; }

    } if (hasLetter && hasDigit && hasUpperCase && !hasSpace) {
        System.out.println("Your password is strong."); 
    } if (!hasDigit) {      
                System.out.println("You need to put numbers in your password."); 
    } if (!hasUpperCase) {
        System.out.println("You need to use an upper case letter in your password."); 
    } if (hasSpace) {
                    System.out.println("You need to delete any spaces in your password."); 
        } // if we use if statements (and not any "else" or "else if", we get to show all the possible error messages.
    }
}

在更正了“regex”和“regexx”之后,在编译之后,当我输入一个完全适用的密码时,它仍然会显示密码需要大写,也需要数字

问题可能是因为您没有将“regex”放在引号中。它们是弦

试试看:

String redex = "(^[0-9]+$)";
String redexx = "(^[A-Z]+$)";
试试这个:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a password please.");
        String password = scanner.nextLine();

        boolean validLength = password.length() >= 8;
        boolean hasLetter = password.matches(".*[a-zA-Z].*");
        boolean hasDigit = password.matches(".*\\d.*");
        boolean hasSpace = password.matches(".*\\s.*");

        if (!validLength)
            System.out.println("The password must be at least 8 characters long.");
        else if (!hasLetter)
            System.out.println("The password must contain at least one letter.");
        else if (!hasDigit)
            System.out.println("The password must contain at least one digit.");
        else if (hasSpace)
            System.out.println("The password must not contain spaces.");
        else
            System.out.println("Your password is strong.");
    }
}
除了问题注释中提到的错误之外,您还使用了不正确的正则表达式。有关如何在Java中使用正则表达式的信息,请阅读


此外,空格字符实际上使密码更强大,因此我建议允许用户输入空格字符。在我的答案的演示代码中,用户不允许输入空格字符,因为这是您的要求。

您是指正则表达式吗?您理解为什么这些行不编译吗?字符串redex=(^[0-9]+$);字符串redexx=(^[A-Z]+$)
password.length()>8
表示密码必须至少为9个字符才能通过该测试,而不是在您稍后写3行时为8个字符。说明“我的代码不工作”的问题必须包括它不工作的原因,以及发生的任何错误,包括编译错误(如果不工作的代码不编译)。您可以只使用一个正则表达式并根据匹配返回一个命名组,然后使用switch语句将组的名称与每个大小写进行比较,以返回一个适当的值:
^(?((((?{0,7})|)(?..*)|(?\D*)(?[^\v\p{Lu}*)|(?[^\v\p{Ll}]*))|((?。{8,}))$/code>。注意:第一个条目是唯一有效的密码,其他所有条目均不符合您的规范。这可能不是最好的答案,但它确实用最少的代码解决了手头的问题。