For JAVA ONLY PLEASE!! 3) Having a secure password is a very important practice,
ID: 3766884 • Letter: F
Question
For JAVA ONLY PLEASE!!
3) Having a secure password is a very important practice, when much of our information is stored online. Write a program that validates a new password, following these rules:
must be 8 characters long
at least one upper case letter and one lower case letter
at least on digit
Write a program that asks for a password, than asks again to confirm it. If the passwords do not match or the rules are not fulfilled, prompt again. Your program should include a method that checks whether a password is valid.
If you could also print the outputs I would greatly appericate it , Thank you !!
Explanation / Answer
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Srinivas Palli
*
*/
public class PasswordValidator {
private static Pattern pattern;
private static Matcher matcher;
private static final String PASSWORD_PATTERN = "((?=.*[a-z])(?=.*\d)(?=.*[A-Z]).{8,40})";
/**
* @param password
* @return
*/
public static boolean validate(final String password) {
pattern = Pattern.compile(PASSWORD_PATTERN);
matcher = pattern.matcher(password);
return matcher.matches();
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print("Enter the Password:");
String password=scanner.nextLine();
System.out.print("Confirm Password:");
String confirmPassword=scanner.nextLine();
if(password.equals(confirmPassword)){
if(validate(password))
System.out.println("Password is Valid");
else
System.out.println("Password is Invalid");
}else{
System.out.println("Password and Confirm Password does not match");
}
}
}
OUTPUT:
Test1:
Enter the Password:hi2Bkihertg
Confirm Password:hi2Bkihertg
Password is Valid
Test2:
Enter the Password:wes
Confirm Password:re
Password and Confirm Password does not match
Test3:
Enter the Password:erddfr
Confirm Password:erddfr
Password is Invalid
Test3:
Enter the Password:Wsdffggfghy
Confirm Password:Wsdffggfghy
Password is Invalid
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.