Search code examples
javajava.util.scannercustom-exceptionspassword-checker

No Output for password checker with custom exception Java


I am trying to make a program that checks a password from user input. The password criteria are that it has at least 10 characters, where at least 1 is a digit, 1 is a lower case and one is an upper case letter. For the assignment I have to create a custom exception class which is what is first below. I then have to create a method in a second class that checks each criteria and throws the exception with the proper error message. I have been trying for hours but for some reason my program will not print anything at all and I was hoping a fresh pair of eyes could help point me in the right direction. I am not looking for a handout, I just have a very weak grasp of custom exceptions (I have read the API, and my class book as well as gone to class everyday so its not that I'm not trying.)

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

}
}

I have tried running it through a visualizer but it will get to where I should input something and display a NoSuchElement error, while my command prompt does not, it simply wont display any messages after I input a password.


Solution

  • Is this what you're looking for?

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