gpt4 book ai didi

java - 密码检查程序 - 检查大写字母、小写字母、数字和特殊字符

转载 作者:行者123 更新时间:2023-12-01 21:58:50 25 4
gpt4 key购买 nike

我有一项作业要求我创建一个密码检查程序。

密码必须至少8个字符,包含大小写字母、数字和特殊字符。

我相信我已经接近解决这个问题,但我的技能仍在发展,但我遇到了困难。

package project1;

/**
*
* @author danechristian
*/
import java.util.*;
public class Project1
{
static Scanner console = new Scanner(System.in);
static final String SPECIAL_CHARACTERS = "!,#,$,%,^,&,*,|";
static String password;

public static void main(String[] args)
{

System.out.println("Create a password: ");
password = console.next();

if (validPassword(password))
{
System.out.println("Password Saved");
}
else
{
System.out.println("Invalid Passowrd. Password "
+ "must contain atleast 1 capital letter"
+ "1 lower case letter, 1 digit, 1"
+ "special character (!#$%^&*|) and "
+ "be atleast 8 characters long");
}
}

public static boolean validPassword(String password)
{
boolean upCase = false;
boolean loCase = false;
boolean isDigit = false;
boolean spChar = false;

if (password.length() >= 8)
{
for (int i = 0; i < password.length() - 1; i++)
{
if (Character.isUpperCase(password.charAt(i)))
{
upCase = true;
}

if (Character.isLowerCase(password.charAt(i)))
{
loCase = true;
}

if (Character.isDigit(password.charAt(i)))
{
isDigit = true;
}

if (SPECIAL_CHARACTERS.contains(password))
{
spChar = true;
}
}
}
return (upCase && loCase && isDigit && spChar);
}

}

最佳答案

为了检查是否有这样的内容:

public static boolean validPassword(String password){
boolean upCase = false;
boolean loCase = false;
boolean isDigit = false;
boolean spChar = false;
if (password.length()>7){
if (password.matches(".+[A-Z].+")){
upCase = true;
}
if (password.matches(".+[a-z].+")){
loCase = true;
}
if (password.matches(".+[1-9].+")){
isDigit = true;
}
if (SPECIAL_CHARACTERS.contains(password)){
spChar = true;
}
}
return (upCase && loCase && isDigit && spChar);
}

关于java - 密码检查程序 - 检查大写字母、小写字母、数字和特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33965542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com