Advertisement
CoineTre

JF-ExcMethods04.Password Validator

Feb 12th, 2021
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Exc4PasswordValidator {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String input = scanner.nextLine();
  7.         if(getPasswordCharacters(input) && checkLettersDigits(input)&& checkTwoDigits(input)){
  8.             System.out.println("Password is valid");
  9.         }
  10.         if (!getPasswordCharacters(input)) {
  11.             System.out.println("Password must be between 6 and 10 characters");
  12.         }
  13.         if (!checkLettersDigits(input)){
  14.             System.out.println("Password must consist only of letters and digits");
  15.         }
  16.         if (!checkTwoDigits(input)){
  17.             System.out.println("Password must have at least 2 digits");
  18.         }
  19.     }
  20.  
  21.     private static boolean getPasswordCharacters(String input) {
  22.         return input.length() >= 6 && input.length() <= 10;
  23.     }
  24.     private static boolean checkLettersDigits(String input){
  25.         for (int i = 0; i < input.length(); i++) {
  26.             char index = input.charAt(i);
  27.             boolean digit = Character.isDigit(index);
  28.             boolean symbol = Character.isLetter(index);
  29.             if (!digit && !symbol){
  30.                 return false;
  31.             }
  32.         }
  33.         return true;
  34.     }
  35.  
  36.     private static boolean checkTwoDigits(String input) {
  37.         int count = 0;
  38.         for (int i = 0; i <input.length() ; i++) {
  39.             char index = input.charAt(i);
  40.             if (Character.isDigit(index)){
  41.                 count++;
  42.             }
  43.             if (count == 2){
  44.                 return true;
  45.             }
  46.         }
  47.         return false;
  48.     }
  49. }
  50.  
  51.  
  52. /*Write a program that checks if a given password is valid. Password rules are:
  53. • 6 – 10 characters (inclusive);
  54. • Consists only of letters and digits;
  55. • Have at least 2 digits.
  56. If a password is valid, print "Password is valid". If it is not valid,
  57.  for every unfulfilled rule print a message:
  58. • "Password must be between 6 and 10 characters";
  59. • "Password must consist only of letters and digits";
  60. • "Password must have at least 2 digits".
  61. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement