borovaneca

PasswordValidator

Nov 30th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. package Fundamentals.Methods.Exercises;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PasswordValidator {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String input = scanner.nextLine();
  10.  
  11.  
  12.         boolean length = length(input);
  13.         boolean invalidSymbols = invalidSymbols(input);
  14.         boolean checkNumbers = checkNumbers(input);
  15.         boolean total = length && invalidSymbols && checkNumbers;
  16.         if (total){
  17.             System.out.println("Password is valid");
  18.         }
  19.     }
  20.  
  21.     public static boolean length(String text) {
  22.  
  23.         if (text.length() < 6 || text.length() > 10) {
  24.             System.out.println("Password must be between 6 and 10 characters");
  25.             return false;
  26.         } else {
  27.             return true;
  28.         }
  29.     }
  30.  
  31.     public static boolean invalidSymbols(String text) {
  32.         String[] invalidSymbols = {"+", "-", "'", "§", "$", "%", "&", "/", "(", ",", ")", "=", "?", "!", "`", ":", ";", "*", "~"};
  33.  
  34.         boolean checked = false;
  35.         for (int i = 0; i < text.length(); i++) {
  36.             for (int j = 0; j < invalidSymbols.length; j++) {
  37.                 String currentSymbol = invalidSymbols[j];
  38.                 char badSymbol = currentSymbol.charAt(0);
  39.                 if (text.charAt(i) == badSymbol) {
  40.                     System.out.println("Password must consist only of letters and digits");
  41.                     checked = true;
  42.                     break;
  43.                 }
  44.             }
  45.             if (checked){
  46.                 break;
  47.             }
  48.         }
  49.         if (checked) {
  50.             return false;
  51.         } else {
  52.             return true;
  53.         }
  54.     }
  55.  
  56.     public static boolean checkNumbers(String text) {
  57.         int count = 0;
  58.         boolean checked = false;
  59.         char[] number = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
  60.         for (int i = 0; i < text.length(); i++) {
  61.             for (int j = 0; j < number.length; j++) {
  62.                 char currentChar = text.charAt(i);
  63.                 char charArr = number[j];
  64.                 if (currentChar == charArr) {
  65.                     count++;
  66.                     if (count == 2) {
  67.                         checked = true;
  68.                         break;
  69.                     }
  70.                 }
  71.             }
  72.             if (checked) {
  73.                 break;
  74.             }
  75.         }
  76.         if (checked) {
  77.             return true;
  78.         } else {
  79.             System.out.println("Password must have at least 2 digits");
  80.             return false;
  81.         }
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment