没有用于自定义异常Java的密码检查器的输出

时间:2017-11-06 00:00:28

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

我正在尝试制作一个程序来检查用户输入的密码。密码标准是它至少有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错误,而我的命令提示符没有,它只是在输入密码后不会显示任何消息。

1 个答案:

答案 0 :(得分: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);
        }
    }