gpt4 book ai didi

java - 带有自定义异常 Java 的密码检查器无输出

转载 作者:行者123 更新时间:2023-11-30 06:26:21 25 4
gpt4 key购买 nike

我正在尝试制作一个程序来检查用户输入的密码。密码的标准是至少有10个字符,其中至少1个是数字,1个是小写字母,1个是大写字母。对于作业,我必须创建一个自定义异常类,如下所示。然后,我必须在第二个类中创建一个方法来检查每个条件并抛出带有正确错误消息的异常。我已经尝试了几个小时,但由于某种原因,我的程序根本不会打印任何内容,我希望一双新的眼睛可以帮助我指明正确的方向。我不是在寻找讲义,我只是对自定义异常的掌握非常薄弱(我已经阅读了 API、我的类(class)书以及每天去上课,所以这并不是我没有尝试。)

public class PasswordException extends Exception {
public PasswordException() {
super("Invalid Password: ");

}

public PasswordException(String problem) {
super("Invalid: " + problem );
}
}
<小时/>
import java.util.Scanner;
public class passwordMaker{

public static boolean validPassword(String passwordIn) throws PasswordException {
boolean lengthCheck = false;
boolean upperCheck = false;
boolean lowerCheck = false;
boolean digitCheck = false;
boolean check = false;
boolean keepGoing = true;
String problem;

for(int i=0;i<passwordIn.length();i++) // This loop tests string
{
char s=passwordIn.charAt(i); // char s represents the index

if(Character.isUpperCase(s)) // This verifies there is a uppercase letter
{
upperCheck = true;
}
if(Character.isLowerCase(s)) // This verifies there is a lowercase letter
{
lowerCheck=true;
}
if(Character.isDigit(s)) // This verifies there is a digit
{
digitCheck = true;
}

if (passwordIn.length() >= 10) // This verifies the password is atleast 6 characters
{
lengthCheck = true;
}

}

do {
//extracts problem
if (upperCheck == false) {
problem = "The password does not have an upper case letter.";
keepGoing = false;
}
else if (lowerCheck == false) {
problem = "The password does not have a lower case letter.";
keepGoing = false;
}
else if (digitCheck == false) {
problem = "The password does not have a digit.";
keepGoing = false;
}
else if (lengthCheck == false) {
problem = "The password is not long enough";
keepGoing = false;
}
else {
problem = "nothing.";
keepGoing = false;
}
}while(keepGoing);


// Tests results of the loop
if(upperCheck == true && lowerCheck == true && digitCheck == true && lengthCheck == true)
{
check=true;
}

if (check = true) {
return true;
}
else {
throw new PasswordException("the password needs" + problem);


}
}

public static void main(String[] args) {

System.out.println("Enter a password.");
Scanner sc = new Scanner(System.in);

String password = sc.next();

try {
validPassword(password);

}
catch (PasswordException e) {
System.out.println(e.getMessage());
}

}
}

我尝试通过可视化工具运行它,但它会到达我应该输入内容的位置并显示 NoSuchElement 错误,而我的命令提示符不会,在我输入密码后它不会显示任何消息。

最佳答案

这是您要找的吗?

public static void main(String[] args) throws Exception {
System.out.println("Enter a password.");
Scanner sc = new Scanner(System.in);

String password = sc.next();

try {
validatePassword(password);
} catch (PasswordException e) {
System.out.println(e.getMessage());
}
}

static void validatePassword(String password) throws PasswordException {
if (password.length() < 10) {
throw new PasswordException("Password length is less than 10");
}

boolean upperCheck = false;
boolean lowerCheck = false;
boolean digitCheck = false;
for (char c : password.toCharArray()) {
if (Character.isUpperCase(c)) // This verifies there is a uppercase letter
{
upperCheck = true;
}

if (Character.isLowerCase(c)) // This verifies there is a lowercase letter
{
lowerCheck = true;
}
if (Character.isDigit(c)) // This verifies there is a digit
{
digitCheck = true;
}
}

if (!upperCheck) {
throw new PasswordException("There must be an uppercase character");
}

if (!lowerCheck) {
throw new PasswordException ("There must be a lowercase character");
}

if (!digitCheck) {
throw new PasswordException ("There must a be a digit");
}

System.out.println("Valid password.");
}

static class PasswordException extends Exception {

public PasswordException() {
super("Invalid password");
}

public PasswordException(String message) {
super("Invalid password: " + message);
}
}

关于java - 带有自定义异常 Java 的密码检查器无输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47127947/

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