Question: CODE IN JAVA Improve the design of the program so each check is a separate method. Add a loop to check multiple passwords. For our
CODE IN JAVA
Improve the design of the program so each check is a separate method. Add a loop to check multiple passwords. For our program, we want to make certain that the validity of a password passes the following rules:
// Let's say a valid password consist of the following:
//a length of 7-9 characters
// at least one capital letter
//a small letter at location 1
// at least two special characters (something other than a number or digit)
// no underscores or dashes
--------------------Desired Output----------------
Sample output for CheckValid
The password must have:
A length of 7-9 characters
At least one capital letter
A small letter as the second character
At least two special characters (something other than a number or digit)
No underscores or dashes
Enter in a password.
Hs&&dfd
Password is valid
Want to check another? (true or false)
true
The password must have:
A length of 7-9 characters
At least one capital letter
A small letter as the second character
At least two special characters (something other than a number or digit)
No underscores or dashes
Enter in a password.
HH$$dfd
Password does not have a lower case letter as the second letter
Want to check another? (true or false)
true
The password must have:
A length of 7-9 characters
At least one capital letter
A small letter as the second character
At least two special characters (something other than a number or digit)
No underscores or dashes
Enter in a password.
Hsssdfd
Password does not have any at least two special characters (something other than a number or digit)
Want to check another? (true or false)
true
The password must have:
A length of 7-9 characters
At least one capital letter
A small letter as the second character
At least two special characters (something other than a number or digit)
No underscores or dashes
Enter in a password.
Hs-ssdfd
Password does not have any at least two special characters (something other than a number or digit)
Want to check another? (true or false)
true
The password must have:
A length of 7-9 characters
At least one capital letter
A small letter as the second character
At least two special characters (something other than a number or digit)
No underscores or dashes
Enter in a password.
HS---dddfd
Password does not fit the size
Want to check another? (true or false)
false
--------------------------What I have so far---------------------------------------------------------------------
public class Ex3CheckValid {
// Let's say a valid password consist of the following:
//a letter at location 0
//a number at location 1
//a dash at location 2
// a special character (something other than a number or digit) at location 3
// 3 letters - locations 4,5, and 6
// How would I check for a valid password?
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
/*
* start loop
*/
System.out.println("Put in a password");
String pass = scan.nextLine();
// call methods to see if it is valid
boolean isValid = valid(pass);
boolean passLength = valid(pass);
boolean checkFirstLoc = valid(pass);
boolean checkSecondLoc = valid(pass);
boolean checkThirdLoc = valid(pass);
//print out the results
if (isValid&&passLength&&checkFirstLoc&&checkSecondLoc&&checkThirdLoc == true)
System.out.println("Password is valid");
else
System.out.println("Invalid"); //what makes it invalid?, print out why it didnt pass
/*
* end loop
*/
}
//Method that checks length
public static boolean passLength(String code) {
if (code.length() !=7)
return false;
else
return true;
}
//Method that checks 1st location
public static boolean checkFirstLoc(String code) {
char loc1 = code.charAt(1);
if (!Character.isDigit(loc1))
return false;
else
return true;
}
//Method that checks 2nd location
public static boolean checkSecondLoc(String code) {
char loc2 = code.charAt(2);
if (loc2 !='-')
return false;
else
return true;
}
//Method that checks the 3rd location
public static boolean checkThirdLoc(String code) {
char loc3 = code.charAt(3);
if (Character.isLetterOrDigit(loc3) )
return false;
else
return true;
}
public static boolean valid(String code){
char loc0 = code.charAt(0);
char loc4= code.charAt(4);
char loc5 = code.charAt(5);
char loc6 = code.charAt(6);
if (!Character.isLetter(loc0) || !Character.isLetter(loc4)||
!Character.isLetter(loc5)||!Character.isLetter(loc6))
return false;
else
return true;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
