Hey guys I need this to be done in Java, having a little trouble on where to to
ID: 3848159 • Letter: H
Question
Hey guys I need this to be done in Java, having a little trouble on where to to put my method and the missing custom exception
Here is the original question if it helps!
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.
Feedback From Teacher "Your make-up work contains custom exception but you missing custom exception on passwordChecker method and
missing passwordChecker method call within the try block."
My program
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;
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
while (valid == 0 && count < 3){
valid = 1;
System.out.println("Enter Password:");
passwd = sc.next();
if (passwrdLengthCheck(passwd)){
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;
}
if (passwrdChecker(passwd)) {
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){
try {
throw new ValidPassException("Failure of amount of tries sorry!");
} catch (ValidPassException vpe) {
System.out.println(vpe.getMessage());
}}
else{
System.out.println("Success");
}
}
public static boolean passwrdLengthCheck(String passwrd){
return (passwrd.length() < 6 || passwrd.length() > 12);
}
public static boolean passwrdChecker(String passwrd){
Pattern pattern = Pattern.compile("[a-zA-Z0-9]*");
Matcher matcher = pattern.matcher(passwrd);
return (!(passwrd.matches(".*[a-z].*") || passwrd.matches(".*[A-Z].*")) || !passwrd.matches(".*[0-9].*")
|| matcher.matches());
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------
public class ValidPassException extends Exception{
public ValidPassException(String Message){
super(Message);
}
}
Explanation / Answer
The desired program is given below, here i have included the required method in the try block.
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;
@SuppressWarnings("resource")
Scanner sc=new Scanner(System.in);
while (valid == 0 && count < 3){
try{
valid = 1;
System.out.println("Enter Password:");
passwd = sc.next();
if (passwrdLengthCheck(passwd)){
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;
}
if (passwrdChecker(passwd)) {
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){
throw new ValidPassException("Failure of amount of tries sorry!");
}
else{
System.out.println("Success");
}
}
catch(ValidPassException vpe)
{
System.out.println(vpe.getMessage());
}
}
}
public static boolean passwrdLengthCheck(String passwrd){
return (passwrd.length() < 6 || passwrd.length() > 12);
}
public static boolean passwrdChecker(String passwrd){
Pattern pattern = Pattern.compile("[a-zA-Z0-9]*");
Matcher matcher = pattern.matcher(passwrd);
return (!(passwrd.matches(".*[a-z].*") || passwrd.matches(".*[A-Z].*")) || !passwrd.matches(".*[0-9].*")
|| matcher.matches());
}
}