Advertisement
mirozspace

Check password

Feb 12th, 2019
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class Pr4PasswordValidator {
  6.     public static void main(String[] args) throws IOException {
  7.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  8.         String password = reader.readLine();
  9.  
  10.         if (checkNumbersOfCharaptes(password) && checkAllowedSymbols(password) && checkNumberOfDigits(password)) {
  11.             System.out.println("Password is valid");
  12.             return;
  13.         }
  14.         if (!(checkNumbersOfCharaptes(password))) {
  15.             System.out.println("Password must be between 6 and 10 characters");
  16.             if (!(checkNumberOfDigits(password))) {
  17.                 System.out.println("Password must have at least 2 digits");
  18.             }
  19.             return;
  20.         }
  21.         if (!(checkAllowedSymbols(password))) {
  22.             System.out.println("Password must consist only of letters and digits");
  23.         }
  24.         if (!(checkNumberOfDigits(password))) {
  25.             System.out.println("Password must have at least 2 digits");
  26.         }
  27.     }
  28.  
  29.     private static boolean checkNumbersOfCharaptes(String password) {
  30.         boolean checkNumbersOfCharaptes = false;
  31.         if (password.length() >= 6 && password.length() <= 10) {
  32.             checkNumbersOfCharaptes = true;
  33.         }
  34.         return checkNumbersOfCharaptes;
  35.     }
  36.  
  37.     private static boolean checkAllowedSymbols(String password) {
  38.         boolean checkSymbols = false;
  39.         int countLetters = 0;
  40.         int coutDigits = 0;
  41.         for (int i = 0; i < password.length(); i++) {
  42.             //Digits
  43.             int digit = password.charAt(i) - '0';
  44.             if (digit >= 0 && digit <= 9) {
  45.                 coutDigits++;
  46.             }
  47.             //Letters
  48.             if ((password.charAt(i) >= 65 && password.charAt(i) <= 90) ||
  49.                     (password.charAt(i) >= 97 && password.charAt(i) <= 122)) {
  50.                 countLetters++;
  51.             }
  52.         }
  53.         //Result
  54.         if (countLetters > 0 && coutDigits > 0) {
  55.             checkSymbols = true;
  56.         }
  57.         return checkSymbols;
  58.     }
  59.  
  60.     private static boolean checkNumberOfDigits(String password) {
  61.         boolean checkNumberOfDigits = true;
  62.         int coutDigits = 0;
  63.         for (int i = 0; i < password.length(); i++) {
  64.             if (password.charAt(i) >= 48 && password.charAt(i) <= 57) {
  65.                 coutDigits++;
  66.             }
  67.         }
  68.         if (coutDigits <= 2) {
  69.             checkNumberOfDigits = false;
  70.         }
  71.         return checkNumberOfDigits;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement