【问题标题】:No Output for password checker with custom exception Java密码检查器没有自定义异常 Java 的输出
【发布时间】:2018-04-18 02:02:18
【问题描述】:

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

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 错误,而我的命令提示符没有,在我输入密码后它根本不会显示任何消息。

【问题讨论】:

  • 你可以使用正则表达式吗?
  • 我们还没有了解它,所以我的教授不希望我们这样做。
  • if (check = true) 不会像您认为的那样做。应该是if (check),或者如果你必须是详细的if (check == true)。您可能还应该使用|| 而不是&amp;&amp;,并且您应该使用一个好的IDE。

标签: java java.util.scanner custom-exceptions password-checker


【解决方案1】:

这是你要找的吗?

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);
        }
    }

【讨论】:

  • 是的!确切地!谢谢,现在让我比较一下我的代码,看看我做错了什么。
猜你喜欢
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-20
  • 2017-02-28
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多