Hey guys I need help with this to be done in Java! It needs a little bit of crea
ID: 3844446 • Letter: H
Question
Hey guys I need help with this to be done in Java! It needs a little bit of creativity with the password prompt or warning message, for ex, (message saying "You have one more try before we lock you out!) Thanks for the help!! :)
Write Week03_yourName_Homework.java which contains your own customized exception to handle password.
Valid password will include as following;
1. Minimum length will be at least 6 and maximum length will be 12.
2. Should contains character and number and at least 1 or more special character(! - )).
Your program prompt to enter the password maximum of 3 times.
Your program will display either "Success" or "Fail"
Don't forget user friendly prompt message, error message, warning message.
Explanation / Answer
The program is as follows:
import java.io.*;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo{
public static void main(String args[]){
int valid;
int count;
String passwd;
valid = 0;
count = 0;
Scanner sc=new Scanner(System.in);
while (valid == 0 && count < 3){
valid = 1;
System.out.println("Enter Password:");
passwd = sc.next();
if ( passwd.length() < 6 || passwd.length() > 12){
System.out.println("Failure");
System.out.println("Password length should be from 6 to 12 characters");
if (count < 2)
System.out.println("You have " + (2-count) + "trials left before your account will be locked");
count++;
valid = 0;
continue;
}
Pattern pattern = Pattern.compile("[a-zA-Z0-9]*");
Matcher matcher = pattern.matcher(password);
if (!(passwd.matches(".*[a-z].*") || passwd.matches(".*[A-Z].*")) || !passwd.matches(".*[0-9].*")
|| matcher.matches()) {
System.out.println("Failure");
System.out.println("Password should contain charcter, number and atleast 1 or more special character");
System.out.println("You have " + (2-count) + "trials left before your account will be locked");
count++;
valid = 0;
continue;
}
}
if (valid == 0 && count == 3)
System.out.println("Your account gets locked");
else
System.out.println("Success");
}
}