desislava_topuzakova

6. Валидиране на парола

Feb 13th, 2022
1,583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. package Methods;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PasswordValidator_06 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         String password = scanner.nextLine();
  9.  
  10.         //метод, който проверява дали дължината е валидна -> true (валидна), false (невалидна)
  11.         if (!isValidLength(password)) {
  12.             System.out.println("Password must be between 6 and 10 characters");
  13.         }
  14.         //метод, който проверява само букви и цифри -> true (вярно), false (не вярно)
  15.         if (!isValidContent(password)) {
  16.             System.out.println("Password must consist only of letters and digits");
  17.         }
  18.         //метод, който проверява брой на цифрите >= 2 -> true (бр >= 2); false (бр < 2)
  19.         if (!isValidCountDigits(password)) {
  20.             System.out.println("Password must have at least 2 digits");
  21.         }
  22.  
  23.         //валидна -> дължината е валидна, съдържанието е правилно и бр. цифрите >= 2
  24.         if (isValidLength(password) && isValidContent(password) && isValidCountDigits(password)) {
  25.             System.out.println("Password is valid");
  26.         }
  27.  
  28.     }
  29.     public static boolean isValidLength (String password) {
  30.         return password.length() >= 6 && password.length() <= 10;
  31.     }
  32.  
  33.     public static boolean isValidContent (String password) {
  34.         for (int position = 0; position <= password.length() - 1; position++) {
  35.             char currentSymbol = password.charAt(position);
  36.             if (!Character.isLetterOrDigit(currentSymbol)) {
  37.                 return false;
  38.             }
  39.         }
  40.         return true;
  41.     }
  42.  
  43.     public static boolean isValidCountDigits (String password) {
  44.         int count = 0; //брой на цифри
  45.         for (int position = 0; position <= password.length() - 1; position++) {
  46.             char currentSymbol = password.charAt(position);
  47.             if (Character.isDigit(currentSymbol)) {
  48.                 count++;
  49.             }
  50.         }
  51.  
  52.         //знам брой на цифрите
  53.         return count >= 2;
  54.     }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment