Program continue looping until a valid password is entered


I would like to make this program continue looping until a valid password is entered. While this isn't required I still think its needed.

Requirements
Main Method
Implement a program that reads in a user password and verifies it meets the following criteria:
Is at least 8 characters long
Contains at least 1 lower letter character
Contains at least 1 upper letter character
Contains at least 1 numeric digit
Contains at least 1 special character from the set: !@#$%^&*
Does not contain the word "and" or the word "end"
Output a string that prompts the user for a password and include the requirements above in your output.
Output a string that states valid or invalid. If invalid, state which rule above has not been met.
Utilize the following functionality:
indexOf
Looping structure
charAt()
isDigit()
isUpperCase()
isLowerCase()
? and any additional functionality needed.


Code so far
import java.util.Scanner;
public class Project2
{
public static void main(String args[]){
String password;
int len, upper = 0, lower = 0, number = 0, charac = 0, i;
boolean substr;
Scanner in = new Scanner(System.in);
System.out.println("Enter a password that meets the following rules:n" +
"1. Is at least 8 characters longn" +
"2. Contains at least 1 lower letter charactern" +
"3. Contains at least 1 upper letter charactern" +
"4. Contains at least 1 numeric digitn" +
"5. Contains at least 1 special character from the set: !@#$%^&*n" +
"6. Does not contain the word and or the word endn");
System.out.print ("Password: ");
password = in.next();
len = password.length();
for(i = 0; i < len; i++){
char x = password.charAt(i);
if(Character.isUpperCase(x)) upper++;
if(Character.isLowerCase(x)) lower++;
if(Character.isDigit(x)) number++;
if(x == '!' || x == '@' || x == '#' || x == '

|| x == '%' || x == '^' || x == '&' || x == '*') charac++;

 

}
if(password.indexOf("and") == 1 || password.indexOf("end") == 1) substr = false;
else substr = true;
if(len < 8 || upper < 1 || lower < 1 || number < 1 || charac < 1 || substr == false)

{
System.out.println("Invalid!!");
if(len < 8) System.out.println("Must be at least 8 characters long.");
if(upper < 1) System.out.println("There should be atleast 1 uppercase character.");
if(lower < 1) System.out.println("There should be atleast 1 lowercase character.");
if(number < 1) System.out.println("Must contain a numeric digit");
if(charac < 1) System.out.println("Missing a special character");
if(substr == false) System.out.print("The password should not contain the word "and" or "end".");
}
else System.out.println("Valid!!");
}
}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Program continue looping until a valid password is entered
Reference No:- TGS02361461

Now Priced at $10 (50% Discount)

Recommended (99%)

Rated (4.3/5)