Knowee
Questions
Features
Study Tools

Write a Java program that implements a password validator using a custom exception InvalidPasswordException. The program should have a method validatePassword that takes a string as an argument representing a password and checks its validity based on the following rules: The password should be at least 12 characters long.The password should be a combination of uppercase and lowercase letters, numbers, and symbols.If the password does not meet these criteria, the validatePassword method should throw an InvalidPasswordException with an appropriate error message. Provide 5 sample inputs and their corresponding expected outputs to demonstrate the functionality of the validatePassword method.Expected Output:For a valid password, the output should be "Password is valid."For a password that is too short, the output should be "Invalid Password: Password should be at least 12 characters long."For a password without required characters, the output should be "Invalid Password: Password should be a combination of uppercase and lowercase letters, numbers, and symbols."Sample Input:"Strong@Password123"Sample Output:Password is valid.

Question

Write a Java program that implements a password validator using a custom exception InvalidPasswordException. The program should have a method validatePassword that takes a string as an argument representing a password and checks its validity based on the following rules: The password should be at least 12 characters long.The password should be a combination of uppercase and lowercase letters, numbers, and symbols.If the password does not meet these criteria, the validatePassword method should throw an InvalidPasswordException with an appropriate error message. Provide 5 sample inputs and their corresponding expected outputs to demonstrate the functionality of the validatePassword method.Expected Output:For a valid password, the output should be "Password is valid."For a password that is too short, the output should be "Invalid Password: Password should be at least 12 characters long."For a password without required characters, the output should be "Invalid Password: Password should be a combination of uppercase and lowercase letters, numbers, and symbols."Sample Input:"Strong@Password123"Sample Output:Password is valid.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Java program that implements a password validator using a custom exception InvalidPasswordException.

public class Main {
    public static void main(String[] args) {
        try {
            validatePassword("Strong@Password123");
            System.out.println("Password is valid.");
        } catch (InvalidPasswordException e) {
            System.out.println(e.getMessage());
        }
    }

    public static void validatePassword(String password) throws InvalidPasswordException {
        if (password.length() < 12) {
            throw new InvalidPasswordException("Invalid Password: Password should be at least 12 characters long.");
        }
        if (!password.matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{12,}$")) {
            throw new InvalidPasswordException("Invalid Password: Password should be a combination of uppercase and lowercase letters, numbers, and symbols.");
        }
    }
}

class InvalidPasswordException extends Exception {
    public InvalidPasswordException(String message) {
        super(message);
    }
}

This program defines a validatePassword method that checks if the password is at least 12 characters long and contains a combination of uppercase and lowercase letters, numbers, and symbols. If the password does not meet these criteria, the method throws an InvalidPasswordException with an appropriate error message.

Here are 5 sample inputs and their corresponding expected outputs:

  1. Input: "Strong@Password123" Output: "Password is valid."
  2. Input: "short" Output: "Invalid Password: Password should be at least 12 characters long."
  3. Input: "alllowercase123@" Output: "Invalid Password: Password should be a combination of uppercase and lowercase letters, numbers, and symbols."
  4. Input: "NOLOWERCASE123@" Output: "Invalid Password: Password should be a combination of uppercase and lowercase letters, numbers, and symbols."
  5. Input: "NoSymbols123" Output: "Invalid Password: Password should be a combination of uppercase and lowercase letters, numbers, and symbols."

This problem has been solved

Similar Questions

Develop a Java program that creates a simple GUI application for a passwordvalidator. The program should allow users to input a password and check ifit meets certain criteria.Requirements:Window with password input, "Check Password" button, result label.Validate password: ≥8 chars, ≥1 uppercase, ≥1 lowercase, ≥1 digit.(7)

Certain authentication system requires the user to enter a password which is an integer. The authentication system should display "Invalid integer. Enter a valid input.",  if the password entered is not a valid integer. This process should be repeated until the user enters a valid integer input. If the password is a valid integer, the authentication system checks if the integer is prime or not. If the password is prime, it displays "Authenticated," otherwise it displays "Unauthenticated”. Write a Java program for the above with proper exception handling.Input-Output Form 1:Enter a valid integer password: 12.3Output:  Invalid integer. Enter a valid input.Enter a valid integer password: 12.7Output: Invalid integer. Enter a valid input.Enter a valid integer password: 12Output: Unauthenticated

Check if a string is matching password requirements Sample inputProgram#123Sample outputSuccessfully Matched with the Password Requirements.ExplanationRead a string which should satisfy the password requirements.If the string satisfies the given requirements print "Successfully Matched with the Password Requirements." without double quotes.if not satisfied print "Not Matched with the Password Requirements." without the double quotes.Password Requirements :Atleast one Uppercase character.Atleast one Lowercase character.Atleast one special Character.Atleast one Number.The string should be more than 8 characters long. SAMPLE INPUT:  Program#12SAMPLE OUTPUT:Successfully Matched with the Password Requirements.Note:Your code must be able to print the sample output from the provided sample input. However, your code is run against multiple hidden test cases. Therefore, your code must pass these hidden test cases to solve the problem statement.

Develop a python function using regular expression for validating a password.Password should contain at least one capital letter, one number and one special character like @$!%*#?&Length of the password should be between 8 and 18.Sample Input:XdsE83&!2Sample Output:ValidSample Input:asDf#$Q$$Invalid

special Character.Atleast one Number.The string should be more than 8 characters long. SAMPLE INPUT:  Program#12SAMPLE OUTPUT:Successfully Matched with the Password Requirements.Note:Your code must be able to print the sample output from the provided sample input. However, your code is run against multiple hidden test cases. Therefore, your code must pass these hidden test cases to solve the problem statement.LimitsTime Limit: 5.0 sec(s) for each input fileMemory Limit: 256 MBSource Limit: 1024 KBScoringScore is assigned if any testcase pas

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.