daily pastebin goal
43%
SHARE
TWEET

Untitled

a guest Feb 18th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class PasswordValidator2 {
  6.     public static void main(String[] args) throws IOException {
  7.  
  8.         BufferedReader reader =
  9.                 new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         String password = reader.readLine();
  12.  
  13.         if (!isInRange(password)){
  14.             System.out.println("Password must be between 6 and 10 characters");
  15.         }
  16.         if (!isLetterOrDigit(password)){
  17.             System.out.println("Password must consist only of letters and digits");
  18.         }
  19.         if (!haveAtLeast2Digit(password)){
  20.             System.out.println("Password must have at least 2 digits");
  21.         }
  22.         if (isInRange(password) && isLetterOrDigit(password) && haveAtLeast2Digit(password)){
  23.             System.out.println("Password is valid");
  24.         }
  25.     }
  26.     private static boolean isInRange(String password) {
  27.         if (password.length() >= 6 && password.length() <= 10) {
  28.             return true;
  29.         }
  30.         return false;
  31.     }
  32.     private static boolean isLetterOrDigit(String password) {
  33.         for (int i = 0; i < password.length(); i++) {
  34.             if (!Character.isLetterOrDigit(password.charAt(i))) {
  35.                 return false;
  36.             }
  37.         }
  38.         return true;
  39.     }
  40.     private static boolean haveAtLeast2Digit(String password) {
  41.         int count = 0;
  42.         for (int i = 0; i < password.length(); i++) {
  43.             if (Character.isDigit(password.charAt(i))) {
  44.                 count++;
  45.             }
  46.         }
  47.         if (count < 2){
  48.             return false;
  49.         }
  50.         return true;
  51.     }
  52. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top