Advertisement
desislava_topuzakova

03. Characters in Range

Jun 13th, 2021
1,185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. import java.util.Locale;
  2. import java.util.Scanner;
  3.  
  4. public class Demo {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         String password = scanner.nextLine();
  8.  
  9.         //проверка за дължина -> от 6 до 10 вкл символа
  10.         //ако дължината ми не е валидна -> print
  11.         boolean isValidLength = isValidLength(password);
  12.         if(!isValidLength) {
  13.             System.out.println("Password must be between 6 and 10 characters");
  14.         }
  15.  
  16.         //проверка дали съдържа само букви и цифри
  17.         boolean isValidContent = isValidContent(password);
  18.         if(!isValidContent) {
  19.             System.out.println("Password must consist only of letters and digits");
  20.         }
  21.  
  22.         //проверка дали има поне 2 цифри
  23.         boolean isValidCount = isValidCountDigits(password);
  24.         if(!isValidCount) {
  25.             System.out.println("Password must have at least 2 digits");
  26.         }
  27.  
  28.         //ако паролата е валидна по всички параметри
  29.         if(isValidLength && isValidContent && isValidCount) {
  30.             System.out.println("Password is valid");
  31.         }
  32.  
  33.  
  34.     }
  35.  
  36.     //метод за проверка на дължината -> true(валидна дължина) и false(невалидна)
  37.     private static boolean isValidLength(String password) {
  38.         //валидна
  39.         if(password.length() >= 6 && password.length() <= 10) {
  40.             return true;
  41.         } else { //невалидна
  42.             return false;
  43.         }
  44.     }
  45.  
  46.     //метод за проверка на съдържание за букви и цифри -> true(валидна) и false(невалидна)
  47.     private static boolean isValidContent(String password) {
  48.         for (int index = 0; index < password.length(); index++) {
  49.             char currentSymbol = password.charAt(index);
  50.             //ако символа ми е различен от буква или цифра
  51.             if(!Character.isLetterOrDigit(currentSymbol)) {
  52.                 return false;
  53.             }
  54.         }
  55.         //обходила всички символи и никой не ми е бил различен от буква или цифра
  56.         return true;
  57.     }
  58.  
  59.     //метод за проверка на броя на цифрите в паролата -> true(валидна) и false(невалидна)
  60.     private static boolean isValidCountDigits (String password) {
  61.         //true -> броя на цифрите >= 2
  62.         //false -> броя на цифрите < 2
  63.         int count = 0;
  64.         for (int index = 0; index < password.length(); index++) {
  65.             char currentSymbol = password.charAt(index);
  66.             //проверка за цифра
  67.             if(Character.isDigit(currentSymbol)) {
  68.                 count++;
  69.             }
  70.         }
  71.  
  72.         //броя на цифрите
  73.         return count >= 2;
  74.  
  75.     }
  76.  
  77.  
  78.  
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement