Didart

Password Validator

Oct 8th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PasswordValidator {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String password = scanner.nextLine();
  8.  
  9.         boolean isPasswordValidLength = isValidLength(password);
  10.         if (!isPasswordValidLength) {
  11.             System.out.println("Password must be between 6 and 10 characters");
  12.         }
  13.  
  14.         boolean isValidPasswordContent = isValidContent(password);
  15.         if (!isValidPasswordContent) {
  16.             System.out.println("Password must consist only of letters and digits");
  17.         }
  18.         boolean isValidPasswordCountDigits = isValidCountDigit(password);
  19.         if (!isValidPasswordCountDigits) {
  20.             System.out.println("Password must have at least 2 digits");
  21.         }
  22.         if (isPasswordValidLength && isValidPasswordContent
  23.                 && isValidPasswordCountDigits) {
  24.             System.out.println("Password is valid");
  25.         }
  26.     }
  27.  
  28.     private static boolean isValidLength(String password) {
  29.  
  30.         return password.length() >= 6 && password.length() <= 10;
  31.     }
  32.  
  33.     private static boolean isValidContent(String password) {
  34.  
  35.         for (char symbol : password.toCharArray()) {    
  36.             if (!Character.isLetterOrDigit(symbol)) {  
  37.                 return false;
  38.             }
  39.         }
  40.         return true;
  41.     }
  42.  
  43.     private static boolean isValidCountDigit(String password) {
  44.  
  45.         int count = 0;
  46.  
  47.         for (char symbol : password.toCharArray()) {
  48.             if (Character.isDigit(symbol)) {
  49.                 count++;
  50.             }
  51.         }
  52.     /*    if (count >= 2) {
  53.             return true;
  54.         } else {
  55.             return false;
  56.         }*/
  57.         return count >= 2;
  58.     }
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment