Guest User

Untitled

a guest
Jan 10th, 2021
102
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class Method {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         String password = scanner.nextLine();
  9.         boolean isValid = false;
  10.  
  11.         boolean firstRule = passwordLength(password, isValid);
  12.         boolean secondRule = passwordConsistency(password, isValid);
  13.         boolean thirdRule = passwordDigits(password, isValid);
  14.  
  15.         if (firstRule && secondRule && thirdRule) {
  16.             System.out.println("Password is valid");
  17.         }
  18.     }
  19.  
  20.     private static boolean passwordLength(String password, boolean isValid) {
  21.  
  22.         if (password.length() >= 6 && password.length() <= 10) {
  23.             isValid = true;
  24.         } else {
  25.             System.out.println("Password must be between 6 and 10 characters");
  26.         }
  27.  
  28.         return isValid;
  29.     }
  30.  
  31.     private static boolean passwordConsistency(String password, boolean isValid) {
  32.         for (int i = 0; i < password.length(); i++) {
  33.             if (Character.isLetter(password.charAt(i)) || Character.isDigit(password.charAt(i))) {
  34.                 isValid = true;
  35.             } else {
  36.                 System.out.println("Password must consist only of letters and digits");
  37.                 break;
  38.             }
  39.         }
  40.  
  41.         return isValid;
  42.     }
  43.  
  44.     private static boolean passwordDigits(String password, boolean isValid) {
  45.         int digitsCounter = 0;
  46.  
  47.         for (int i = 0; i < password.length(); i++) {
  48.             if (Character.isDigit(password.charAt(i))) {
  49.                 digitsCounter++;
  50.             }
  51.         }
  52.  
  53.         if (digitsCounter >= 2) {
  54.             isValid = true;
  55.         } else {
  56.             System.out.println("Password must have at least 2 digits");
  57.         }
  58.  
  59.         return isValid;
  60.     }
  61. }
  62.  
RAW Paste Data