【发布时间】:2017-02-02 14:11:24
【问题描述】:
密码检查程序应该接受用户输入的用户名和密码,并输出密码是否有效。
我一直在尝试为此使用正则表达式,但遇到了问题。该模式适用于我的所有规则,但用户名规则除外。 另外,有没有办法将输出从“true”或“false”更改为自定义?
到目前为止我的代码:
import java.util.regex.*;
import java.util.Scanner;
public class validPassword {
private static Scanner scnr;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Variable Management
String un, pw, req; // Variable Variable
System.out.println("Please enter a username: ");
// ^Need to implement so if it matches the password it's invalid^
un = input.nextLine(); // Gathers user's username input
System.out.println("Please enter a password: ");
pw = input.nextLine(); // Gathers user's password input
req = "(?=.*[0-9])(?=.*[a-zA-Z]).{8,}";
System.out.println(pw.matches(req)); // Can I customize the output?
}
}
感谢您的帮助! :)
【问题讨论】:
-
您知道,在上次编辑中,您将含义从“必须包含数字、小写字母和大写字母”更改为“必须包含数字和小写或大写字母” ?现在可以只使用小写和大写字母。是你不赞成我的回答吗?如果有,为什么?
标签: java regex passwords password-checker