Ivelin_Arsov

Password Validation

Jun 15th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.73 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PasswordValidator {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         String password = scan.nextLine();
  7.  
  8.         printPassHave2Digits(password);
  9.         printPassLegitSymbols(password);
  10.         printPassRange(password);
  11.         boolean isTrue = printPassHave2Digits(password) && printPassLegitSymbols(password) && printPassRange(password);
  12.         if (isTrue){
  13.             System.out.println("Password is valid");
  14.         }else{
  15.  
  16.         }
  17.     }
  18.  
  19.     private static boolean printPassHave2Digits(String input) {
  20.  
  21.         int digitCount = 0;
  22.         for (int i = 0; i < input.length(); i++) {
  23.             char temp = input.charAt(i);
  24.             if (temp >= '0' && temp <= '9') {
  25.                 digitCount++;
  26.             }
  27.         }
  28.         if (digitCount < 2) {
  29.             System.out.println("Password must have at least 2 digits");
  30.             return false;
  31.         }else {
  32.             return true;
  33.         }
  34.     }
  35.  
  36.     private static boolean printPassLegitSymbols(String input) {
  37.         for (int i = 0; i < input.length(); i++) {
  38.             char symbol = input.charAt(i);
  39.             if ((symbol >= 'a' && symbol <= 'z') || (symbol >= 'A' && symbol <= 'Z') || (symbol >= '0' && symbol <= '9')) {
  40.  
  41.             } else {
  42.                 System.out.println("Password must consist only of letters and digits");
  43.                 return false;
  44.             }
  45.         }
  46.         return true;
  47.     }
  48.  
  49.     private static boolean printPassRange(String input) {
  50.         int length = input.length();
  51.         if (length >= 6 && length <= 10) {
  52.             return true;
  53.         } else {
  54.             System.out.println("Password must be between 6 and 10 characters");
  55.             return false;
  56.         }
  57.     }
  58. }
Add Comment
Please, Sign In to add comment