Advertisement
deyanmalinov

04. Password Validator

Feb 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6.         public static void main(String[] args) {
  7.             Scanner scan = new Scanner(System.in);
  8.             String pass = scan.nextLine();
  9.             if (validPass(pass)){
  10.                 System.out.println("Password is valid");
  11.             }
  12.         }
  13.         public static boolean validPass (String s){
  14.             boolean correctCharCount = validCount(s);
  15.             boolean onlyLandD = coutValid(s);
  16.             boolean haveThoDig = twoDigVal(s);
  17.             return correctCharCount && onlyLandD && haveThoDig;
  18.  
  19.         }
  20.  
  21.     private static boolean twoDigVal(String s) {
  22.             int digCout = 0;
  23.         for (int i = 0; i < s.length(); i++) {
  24.             char sim = s.charAt(i);
  25.             if (Character.isDigit(sim)){
  26.                 digCout ++;
  27.                 if (digCout >=2){
  28.                     return true;
  29.                 }
  30.             }
  31.  
  32.         }
  33.         System.out.println("Password must have at least 2 digits");
  34.         return false;
  35.     }
  36.  
  37.     private static boolean coutValid(String s) {
  38.         for (int i = 0; i < s.length(); i++) {
  39.             char simb = s.charAt(i);
  40.             if (!Character.isLetterOrDigit(simb)){
  41.                 System.out.println("Password must consist only of letters and digits");
  42.                 return false;
  43.             }
  44.  
  45.         }
  46.         return true;
  47.     }
  48.  
  49.     private static boolean validCount(String s) {
  50.             boolean isValid = s.length() > 5 && s.length() < 11;
  51.             if (!isValid){
  52.                 System.out.println("Password must be between 6 and 10 characters");
  53.                 return false;
  54.             }
  55.          return true;
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement