Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. /*
  2. This program uses a driver class ValidatePassword to get user input from the screen using the
  3. scanner class. The ValidatePassword calls the PassCheck method that checks to see if the entered
  4. password is valid. The requirments for a valid passwors are as follows:
  5.  
  6. 1) Password must be at least 8 characters long.
  7. 2) Password must contain two capital letters.
  8. 3) Password must contain at least two numbers
  9. 4) Password contains only letters and number
  10.  
  11. If the password is valid the result will display "Valid Password." If not the result will
  12. display an appropriate error message.
  13.  
  14. Author: Daniel
  15. Channel: BasementProgramming
  16.  
  17. */
  18.  
  19. import java.util.Scanner; // Imports the Scanner class to get Keyboard Inputs
  20.  
  21. class ValidatePassword {
  22.  
  23. public static void main (String [] args) {
  24.  
  25. String inputPassword; // Creates the Password variable
  26.  
  27. Scanner input = new Scanner (System.in); // Creates a new Scanner
  28.  
  29. System.out.print("Password: "); // Prints the word "Password" to the screen
  30. inputPassword = input.next(); // Gets the user input for the password
  31.  
  32. System.out.println(PassCheck(inputPassword)); // Calls the PassCheck Method on the password entered by the user and prints result to screen
  33. System.out.println("");
  34.  
  35. main(args); // re-runs the program (Allows for multiple tests)
  36.  
  37. }
  38.  
  39. public static String PassCheck (String Password) {
  40.  
  41. String result = "Valid Password"; // Sets the initial result as valid
  42. int length = 0; // Stores the number characters in the password
  43. int numCount = 0; // Variable used to store numbers in the password
  44. int capCount = 0; // Variable used to store capital letters in the password
  45.  
  46.  
  47. for (int x =0; x < Password.length(); x++) {
  48. if ((Password.charAt(x) >= 47 && Password.charAt(x) <= 58) || (Password.charAt(x) >= 64 && Password.charAt(x) <= 91) ||
  49. (Password.charAt(x) >= 97 && Password.charAt(x) <= 122)) {
  50. //Keep the Password
  51. } else {
  52. result = "Password Contains Invalid Character!"; //Checks that password contains only letters and numbers
  53. }
  54.  
  55. if ((Password.charAt(x) > 47 && Password.charAt(x) < 58)) { // Counts the number of numbers
  56. numCount ++;
  57. }
  58.  
  59. if ((Password.charAt(x) > 64 && Password.charAt(x) < 91)) { // Counts the number of capital letters
  60. capCount ++;
  61. }
  62.  
  63. length = (x + 1); // Counts the passwords length
  64.  
  65. } // Ends the for loop
  66.  
  67. if (numCount < 2){ // Checks that password contains two numbers
  68. result = "Not Enough Numbers in Password!";
  69. }
  70.  
  71. if (capCount < 2) { // Checks that password contains two capital letters
  72. result = "Not Enough Capital Letters in Password!";
  73. }
  74.  
  75. if (length < 8){ // Checks that password is long enough
  76. result = "Password is Too Short!";
  77. }
  78.  
  79. return (result); // Returns the value of "result"
  80.  
  81. } // Ends the PassCheck method
  82. } // Ends the ValidatePassword class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement