Sample Valid Passwords (with valid message): ABC2%def (valid) A3b$cd$2*efg (valid) Sample Invalid Passwords (with invalid error message) : Abcdel# (invalid - must be between 8 and 12 characters) aBc3$defGhijk (invalid - must be between 8 and 12 characters) ABC2%DEF (invalid - must contain at least one lowercase letter) abc2%def (invalid - must contain at least one uppercase letter) ABC$$ DEF (invalid - must contain at least one numerical digit) 3ABC2%def (invalid - cannot begin or end with numeric digit) ABC2%def4 (invalid - cannot begin or end with numeric digit) A3b$cd#25^ef (invalid - cannot contain consecutive numeric digits) aBC2defg (invalid - must contain at least one special character) ABC2%d$^ef (invalid - cannot contain more than three special characters) aBC24d%f (invalid - cannot have repeated special characters)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

This is a program that checks if a password meets certain requirements. I missed some of the instructions and even though it works, whenever an invalid password is entered it does not give the requested output. I am stuck on how to fix it because I get erros trying to change it. (expected output in pictured attached)

public static void main(String[] args) {

Scanner console= new Scanner(System.in);

welcomeMenu();

boolean finished = false;

while(!finished) {

displayMenu();

String response= userInput(console);

finished= getOption(console, response);

}

System.out.println("You are exiting this program. Thanks for usign it. See you later!");

}

//method that takes the user-entered password

public static void gettingPassword(Scanner console) {

System.out.println("Please enter password: ");

String password = console.next();

passwordLength(password);

validatePassword(password);

}

//method that welcomes the user and explain the task achieved by the program

public static void welcomeMenu() {

System.out.println("Welcome to password verification!");

System.out.println("This program determines if your password meet all requirements!");

System.out.println();

}

//method that dispalys the requirements of the password

public static void passwordRequirements() {

System.out.println("\tIt must be in between 8 to 12 characters.");

System.out.println("\tIt must have at least one lower case letter (a to z).");

System.out.println("\tIt must have at least one uppercase letter (A to Z).");

System.out.println("\tIt must contain at least one numerical digit.");

System.out.println("\tIt cannot begin or end with a numertical digit.");

System.out.println("\tIt must contain at least one of these special "

+ "characters (@, #, $, %, ^).");

System.out.println("\tNo more than 3 special characters are allowed.");

System.out.println("\tYou cannot include a special character more than once.");

System.out.println();

}

public static void displayMenu() {

System.out.println("Please enter one of these three options:");

System.out.println("\tD for Displaying Password Requirements. ");

System.out.println("\tV for Verifying New Password.");

System.out.println("\tE to Exist The Program.");

System.out.println();

}

private static String userInput(Scanner console) {

String selection = console.nextLine();

return selection;

}

public static boolean getOption(Scanner console, String optionSelected) {

boolean finished= false;

if (!optionSelected.equalsIgnoreCase("E")) {

if (optionSelected.equalsIgnoreCase("D")) {

passwordRequirements();

}

else if(optionSelected.equalsIgnoreCase("V")) {

gettingPassword(console);

 

 

}

else {

System.out.println("Option entered invalid, please try again!");

}

}

else {

finished= true;

}

return finished;

}

public static boolean passwordLength(String password) {

if (password.length() >= 8 && password.length() <=12) {

return true;

}

else {

System.out.println("Invalid password.\nIt must be in between 8 to 12 characters");

System.out.println();

}

return false;

}

 

// method that checks if the string (password) meets all requirements

public static void validatePassword(String str){

boolean flag =true;

int upperCaseCount = 0, lowerCaseCount = 0, digitCount = 0, specialCharCount = 0;

char ch1 =str.charAt((str.length()-1));

char c = ' ';

        char ch = str.charAt(0);

        if ((ch >= '0' && ch <= '9') || (ch1 >= '0' && ch1 <= '9')){

        System.out.println("Invalid.");

        flag = false;

        }

        else{

        for(int i = 0; i < str.length(); i++) {

        ch = str.charAt(i);

        if (ch >= 'A' && ch <= 'Z'){

        upperCaseCount++;

            }

        

            else if (ch >= 'a' && ch <= 'z'){

                lowerCaseCount++;

            }

            else if (ch >= '0' && ch <= '9'){

                 if(i != (str.length()-1))

                 {

                   ch1 = str.charAt((i + 1));

                   if ((ch >= '0' && ch <= '9') && (ch1 >= '0' && ch1 <= '9')){

                       System.out.println("A");

                       flag = false;

                       break;

                   }

                 }

                 

                digitCount++;

            }

            else{

                if (c==ch){

                    System.out.println("Invalid");

                    flag = false;

                    break;

                }

                else{

                    c=ch;

                }

                specialCharCount++;

               }

          }

        

          if( upperCaseCount == 0 || lowerCaseCount == 0 || digitCount==0 || specialCharCount==0 || specialCharCount > 3)

          {

            System.out.println("Invalid");

            flag = false;

          }

 

        }

        

     if (flag)

     {

        System.out.println("Invalid");

     }

  

  }

Sample Valid Passwords (with valid message):
ABC2%def (valid)
A3b$cd$2*efg (valid)
Sample Invalid Passwords (with invalid error message) :
Abcdel# (invalid - must be between 8 and 12 characters)
aBc3$defGhijk (invalid - must be between 8 and 12 characters)
ABC2%DEF (invalid - must contain at least one lowercase letter)
abc2%def (invalid - must contain at least one uppercase letter)
ABC$$ DEF (invalid - must contain at least one numerical digit)
3ABC2%def (invalid - cannot begin or end with numeric digit)
ABC2%def4 (invalid
- cannot begin or end with numeric digit)
A3b$cd#25^ef (invalid - cannot contain consecutive numeric digits)
aBC2defg (invalid - must contain at least one special character)
ABC2%d$^ef (invalid
- cannot contain more than three special characters)
aBC24d%f (invalid - cannot have repeated special characters)
Transcribed Image Text:Sample Valid Passwords (with valid message): ABC2%def (valid) A3b$cd$2*efg (valid) Sample Invalid Passwords (with invalid error message) : Abcdel# (invalid - must be between 8 and 12 characters) aBc3$defGhijk (invalid - must be between 8 and 12 characters) ABC2%DEF (invalid - must contain at least one lowercase letter) abc2%def (invalid - must contain at least one uppercase letter) ABC$$ DEF (invalid - must contain at least one numerical digit) 3ABC2%def (invalid - cannot begin or end with numeric digit) ABC2%def4 (invalid - cannot begin or end with numeric digit) A3b$cd#25^ef (invalid - cannot contain consecutive numeric digits) aBC2defg (invalid - must contain at least one special character) ABC2%d$^ef (invalid - cannot contain more than three special characters) aBC24d%f (invalid - cannot have repeated special characters)
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 10 images

Blurred answer
Knowledge Booster
InputStream
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education