Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- This program uses a driver class ValidatePassword to get user input from the screen using the
- scanner class. The ValidatePassword calls the PassCheck method that checks to see if the entered
- password is valid. The requirments for a valid passwors are as follows:
- 1) Password must be at least 8 characters long.
- 2) Password must contain two capital letters.
- 3) Password must contain at least two numbers
- 4) Password contains only letters and number
- If the password is valid the result will display "Valid Password." If not the result will
- display an appropriate error message.
- Author: Daniel
- Channel: BasementProgramming
- */
- import java.util.Scanner; // Imports the Scanner class to get Keyboard Inputs
- class ValidatePassword {
- public static void main (String [] args) {
- String inputPassword; // Creates the Password variable
- Scanner input = new Scanner (System.in); // Creates a new Scanner
- System.out.print("Password: "); // Prints the word "Password" to the screen
- inputPassword = input.next(); // Gets the user input for the password
- System.out.println(PassCheck(inputPassword)); // Calls the PassCheck Method on the password entered by the user and prints result to screen
- System.out.println("");
- main(args); // re-runs the program (Allows for multiple tests)
- }
- public static String PassCheck (String Password) {
- String result = "Valid Password"; // Sets the initial result as valid
- int length = 0; // Stores the number characters in the password
- int numCount = 0; // Variable used to store numbers in the password
- int capCount = 0; // Variable used to store capital letters in the password
- for (int x =0; x < Password.length(); x++) {
- if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58) || (Password.charAt(x) >= 64 && Password.charAt(x) <= 91) ||
- (Password.charAt(x) >= 97 && Password.charAt(x) <= 122)) {
- //Keep the Password
- } else {
- result = "Password Contains Invalid Character!"; //Checks that password contains only letters and numbers
- }
- if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) { // Counts the number of numbers
- numCount ++;
- }
- if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) { // Counts the number of capital letters
- capCount ++;
- }
- length = (x + 1); // Counts the passwords length
- } // Ends the for loop
- if (numCount < 2){ // Checks that password contains two numbers
- result = "Not Enough Numbers in Password!";
- }
- if (capCount < 2) { // Checks that password contains two capital letters
- result = "Not Enough Capital Letters in Password!";
- }
- if (length < 8){ // Checks that password is long enough
- result = "Password is Too Short!";
- }
- return (result); // Returns the value of "result"
- } // Ends the PassCheck method
- } // Ends the ValidatePassword class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement