Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. package MethodsExercises;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class PasswordValidator04 {
  6.     static boolean countLetters(String password){
  7.         if (!(password.length()>=6 && password.length()<=10)){
  8.             System.out.println("Password must be between 6 and 10 characters");
  9.             return false;
  10.         }
  11.         return true;
  12.     }
  13.     static boolean charContains(String password){
  14.         for (char i=0; i<password.length();i++) {
  15.             char symbols=password.charAt(i);
  16.  
  17.             if (!Character.isLetterOrDigit(symbols)){
  18.                 System.out.println("Password must consist only of letters and digits");
  19.                 return false;
  20.             }
  21.         }
  22.         return true;
  23.     }
  24.     static boolean twoDigits(String password){
  25.  
  26.         int count=0;
  27.         for (int i = 0; i < password.length() ; i++) {
  28.             char symbols=password.charAt(i);
  29.             if (Character.isDigit(symbols)){
  30.                 count++;
  31.             }
  32.  
  33.         }
  34.         if (count>=2){
  35.             return true;
  36.         }else {
  37.             System.out.println("Password must have at least 2 digits");
  38.             return false;
  39.         }
  40.  
  41.     }
  42.     static void passwordIsValid(String password){
  43.         boolean check1=countLetters(password);
  44.         boolean check2=charContains(password);
  45.         boolean check3=twoDigits(password);
  46.         if (check1&&check2&&check3){
  47.             System.out.println("Password is valid");
  48.         }
  49.     }
  50.     public static void main(String[] args) {
  51.         Scanner scanner=new Scanner(System.in);
  52.         String password=scanner.nextLine();
  53.        
  54.         passwordIsValid(password);
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement