borovaneca

PasswordValidator

Nov 30th, 2022
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 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.         checker(input);
  13.     }
  14.  
  15.     public static void checker(String text){
  16.  
  17.         String[] invalidSymbols = {"+","-","'","§","$","%","&","/","(",",",")","=","?","!","`",":",";","*","~"};
  18.         boolean passwordLength = false;
  19.         if (text.length() < 6 || text.length() > 10){
  20.             passwordLength = true;
  21.         }
  22.         boolean passwordDigits = false;
  23.         for (int i = 0; i < text.length(); i++) {
  24.             for (int j = 0; j < invalidSymbols.length; j++) {
  25.                 String currentSymbol = invalidSymbols[j];
  26.                 char badSymbol = currentSymbol.charAt(0);
  27.                 if (text.charAt(i) == badSymbol){
  28.                     passwordDigits = true;
  29.                     break;
  30.                 }
  31.                 if (passwordDigits){
  32.                     break;
  33.                 }
  34.             }
  35.         }
  36.         boolean passwordNumbers = true;
  37.         int count = 0;
  38.         char[] number = {'1','2','3','4','5','6','7','8','9','0'};
  39.         for (int i = 0; i < text.length(); i++) {
  40.             for (int j = 0; j < number.length; j++) {
  41.                 char currentChar = text.charAt(i);
  42.                 char charArr = number[j];
  43.                 if (currentChar == charArr){
  44.                     count++;
  45.                     if (count == 2) {
  46.                         passwordNumbers = false;
  47.                         break;
  48.                     }
  49.                 }
  50.             }
  51.             if (count == 2){
  52.                 break;
  53.             }
  54.         }
  55.  
  56.         if (passwordLength){
  57.             System.out.println("Password must be between 6 and 10 characters");
  58.         }
  59.         if (passwordDigits){
  60.             System.out.println("Password must consist only of letters and digits");
  61.         }
  62.         if (passwordNumbers){
  63.             System.out.println("Password must have at least 2 digits");
  64.         }
  65.         if (!passwordLength && !passwordDigits && !passwordNumbers){
  66.             System.out.println("Password is valid");
  67.         }
  68.     }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment