gpt4 book ai didi

java - 在Java中检查字符串中的字母大小写(大写/小写)

转载 作者:IT老高 更新时间:2023-10-28 20:35:34 36 4
gpt4 key购买 nike

我遇到的问题是我无法让我的密码验证程序检查一个字符串以确保其中 1 个字符是大写的,一个是小写的,它将检查整个字符串其中一个并根据它正在检查的语句打印错误消息。

我已经查看了这个网站和互联网以寻找答案,但我找不到答案。这是家庭作业。

下面是我当前的代码。

import java.util.Scanner;

public class password
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String password;
String cont = "y";
char ch;
boolean upper = false;
boolean lower = false;

System.out.println("Setting up your password is easy. To view requirements enter Help.");
System.out.print("Enter password or help: ");
password = stdIn.next();
ch = password.charAt(0);

while (cont.equalsIgnoreCase("y"))
{
while (password.isEmpty())
{
System.out.print("Enter password or help: ");
password = stdIn.next();
}

if (password.equalsIgnoreCase("help"))
{
System.out.println("Password must meet these requirements." +
"\nMust contain 8 characters.\nMust contain 1 lower case letter." +
"\nMust contain 1 upper case letter.\nMust contain 1 numeric digit." +
"\nMust contain 1 special character !@#$%^&*\nDoes not contain the word AND or NOT.");

password = "";
}
else if (password.length() < 8)
{
System.out.println("Invalid password - Must contain 8 charaters.");
password = "";
}
else if (!(Character.isLowerCase(ch)))
{
for (int i=1; i<password.length(); i++)
{
ch = password.charAt(i);

if (!Character.isLowerCase(ch))
{
System.out.println("Invalid password - Must have a Lower Case character.");
password = "";
}
}
}
else if (!(Character.isUpperCase(ch)))
{
for (int i=0; i<password.length(); i++)
{
ch = password.charAt(i);

if (!Character.isUpperCase(ch))
{
System.out.println("Invalid password - Must have an Upper Case character.");
password = "";
}
}
}
else
{
System.out.println("Your password is " + password);

System.out.print("Would you like to change your password? Y/N: ");
cont = stdIn.next();
password = "";
}

while (!cont.equalsIgnoreCase("y") && !cont.equalsIgnoreCase("n"))
{
System.out.print("Invalid Answer. Please enter Y or N: ");
cont = stdIn.next();
}
}
}
}

最佳答案

要确定一个字符串是否包含大写和小写字符,可以使用以下方法:

boolean hasUppercase = !password.equals(password.toLowerCase());
boolean hasLowercase = !password.equals(password.toUpperCase());

这允许您检查:

if(!hasUppercase)System.out.println("Must have an uppercase Character");
if(!hasLowercase)System.out.println("Must have a lowercase Character");

本质上,这是通过检查字符串是否等于其完全小写或大写等效来实现的。如果不是这样,则必须至少有一个大写或小写字符。

至于你的其他条件,可以类似的方式满足:

boolean isAtLeast8   = password.length() >= 8;//Checks for at least 8 characters
boolean hasSpecial = !password.matches("[A-Za-z0-9 ]*");//Checks at least one char is not alpha numeric
boolean noConditions = !(password.contains("AND") || password.contains("NOT"));//Check that it doesn't contain AND or NOT

上面有合适的错误信息。

关于java - 在Java中检查字符串中的字母大小写(大写/小写),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16127923/

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