Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Note : Please don t send me AI generated answer. It doesn t help me at all. I need answer which is honestly solved by
Note : Please dont send me AI generated answer. It doesnt help me at all. I need answer which is honestly solved by the tutor.
Password Validation:
The rules passwords are going to follow:
The passwords must be between and characters long
The passwords must contain at least lowercase letter
The passwords must contain at least uppercase letter
The passwords must contain at least number
The passwords must contain at least these special characters: @#$&
The passwords must not contain any whitespace characters
We have to make the following changes in the PasswordValidator.java file.
The PasswordValidator.java file is attached below:
Firstly We need to Create a new custom unchecked exception called InvalidPasswordException. This exception should accept an error message string as input, and display this error message when we print out the error.
Then, We have to Write the logic for the validatePassword method. The method should check all of the rules for passwords. The method should use a StringBuilder to accumulate all of the rules that the password is breaking. The method should then return true if the password is
valid, or throw an InvalidPasswordException with our error message if its not valid.
For this problem, we will use the matches method of the String class. The matches
method will accept a regex pattern string and return a boolean for whether that string
matches the pattern or not. These regex patterns work the same as the patterns we learned for the split method. Theres one small difference, for this problem well need to surround our patterns with wildcards to check if the pattern occurs anywhere in the string.For example, to split out string on any lowercase letters, we would use string.splitaz; To match if our string has any lowercase letters, we would do string.matchesaz;
Lastly we have to Modify the main method so that well handle the exceptions that may occur during password validation. If the exception occurs, catch it and print it out.
PasswordValidator.java:
public class PasswordValidator
public static boolean validatePasswordString password
Your code goes here!
public static void mainString args
String passwords
"password",
password
"PasswordTesting!",
PW$$$$
"Password
"INVALIDPASSWORD!
"Space password
"ValidPasswrd
$VALDPassWord"
;
forString p : passwords
Modify this part to handle exceptions
validatePasswordp;
System.out.printlnp is valid";
System.out.print
;
Sample out put :
password
InvalidPasswordException:
Password must be between and characters
Password must contain an uppercase letter
Password must contain a digit
Password must contain special character
password
InvalidPasswordException:
Password must contain an uppercase letter
PasswordTesting!
InvalidPasswordException:
Password must contain a digit
PW$$$$
InvalidPasswordException:
Password must contain a lowercase letter
Password must contain a digit
Password
InvalidPasswordException:
Password must be between and characters
INVALIDPASSWORD!
InvalidPasswordException:
Password must contain a lowercase letter
Space password
InvalidPasswordException:
Password must not contain whitespace
ValidPasswrd is valid
$VALDPassWord is valid
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started