Advertisement
Guest User

Untitled

a guest
Jul 19th, 2022
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. package MethodsExcercise;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P04PasswordValidator {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String password = scanner.nextLine();
  11.  
  12.         System.out.println(printIfIsValid(password));
  13.         printError(printIfNeedMoreOrLessChars(password));
  14.         printError(printIfNotLettersDigitsOnly(password));
  15.         printError(printIfNeedDigits(password));
  16.     }
  17.     static void printError(String error){
  18.         if (error != "")
  19.             System.out.println(error);
  20.     }
  21.     static String printIfNeedMoreOrLessChars (String password) {
  22.         int length = password.length();
  23.         String letter = "";
  24.         if ((length < 6) || (length > 10)) {
  25.             letter = "Password must be between 6 and 10 characters";
  26.         }
  27.         return letter;
  28.     }
  29.     static String printIfNotLettersDigitsOnly (String password) {
  30.         int length = password.length();
  31.         boolean onlyLettersDigits = true;
  32.         String letter2 = "";
  33.         for (int i = 0; i < length; i++) {
  34.             char currentChar = password.charAt(i);
  35.             if (currentChar < 48) {
  36.                 onlyLettersDigits = false;
  37.             } else if (currentChar > 57 && currentChar < 65) {
  38.                 onlyLettersDigits = false;
  39.             } else if (currentChar > 90 && currentChar < 97) {
  40.                 onlyLettersDigits = false;
  41.             } else if (currentChar > 122) {
  42.                 onlyLettersDigits = false;
  43.             }
  44.  
  45.             if (!onlyLettersDigits) {
  46.                 letter2 = "Password must consist only of letters and digits";
  47.             }
  48.         }
  49.         return letter2;
  50.     }
  51.     static String printIfNeedDigits (String password) {
  52.         int length = password.length();
  53.         int digits = 0;
  54.         String letter3 = "";
  55.         for (int i = 0; i < length; i++) {
  56.             char currentChar = password.charAt(i);
  57.             if (currentChar >= 48 && currentChar <= 57) {
  58.                 digits++;
  59.             }
  60.         }
  61.         if (digits < 2) {
  62.             letter3 = "Password must have at least 2 digits";
  63.         }
  64.  
  65.         return letter3;
  66.     }
  67.     static String printIfIsValid (String password) {
  68.         int length = password.length();
  69.         int digits1 = 0;
  70.         String letter4 = "";
  71.         int onlyLettersDigits1 = 0;
  72.         for (int i = 0; i < length; i++) {
  73.             char currentChar = password.charAt(i);
  74.  
  75.             if (currentChar < 48) {
  76.                 onlyLettersDigits1++;
  77.             } else if (currentChar > 57 && currentChar < 65) {
  78.                 onlyLettersDigits1++;
  79.             } else if (currentChar > 90 && currentChar < 97) {
  80.                 onlyLettersDigits1++;
  81.             } else if (currentChar > 122) {
  82.                 onlyLettersDigits1++;
  83.             }
  84.  
  85.             if (currentChar >= 48 && currentChar <= 57) {
  86.                 digits1++;
  87.             }
  88.  
  89.         }
  90.  
  91.         if (digits1 >= 2) {
  92.             if (length >= 6 && length <= 10) {
  93.                 if (onlyLettersDigits1 == 0) {
  94.                     letter4 = "Password is valid";
  95.                 }
  96.             }
  97.         }
  98.         return letter4;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement