Answered step by step
Verified Expert Solution
Link Copied!

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:
1. The passwords must be between 12 and 25 characters long
2. The passwords must contain at least 1 lowercase letter
3. The passwords must contain at least 1 uppercase letter
4. The passwords must contain at least 1 number
5. The passwords must contain at least 1 these special characters: !@#$%&*()-+=^
6. 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:
1Firstly , 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.
2.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.split([a-z]); To match if our string has any lowercase letters, we would do string.matches(.*[a-z].*);
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 validatePassword(String password){
// Your code goes here!
}
public static void main(String[] args){
String[] passwords ={
"password",
"!!password1111111!",
"PasswordTesting!",
"PW!!!!^^$$$$",
"Password1!",
"INVALIDPASSWORD!1",
"Space password !0",
"ValidPassw0rd!",
"$VAL1D_PassWord"
};
for(String p : passwords){
// Modify this part to handle exceptions
validatePassword(p);
System.out.println(p +" is valid");
System.out.print("
");
}
}
}
Sample out put :
password
InvalidPasswordException:
-Password must be between 12 and 25 characters
-Password must contain an uppercase letter
-Password must contain a digit
-Password must contain special character
!!password1111111!
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
Password1!
InvalidPasswordException:
-Password must be between 12 and 25 characters
INVALIDPASSWORD!1
InvalidPasswordException:
-Password must contain a lowercase letter
Space password !0
InvalidPasswordException:
-Password must not contain whitespace
ValidPassw0rd! is valid
$VAL1D_PassWord is valid

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

identify current issues relating to equal pay in organisations

Answered: 1 week ago