Advertisement
DigiDuncan

PasswordChecker

Nov 18th, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PasswordDriver
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         boolean oKayPass = false;
  8.         String GUISTRING = "***WECLOME TO PASSWORD CHECKER***\n"
  9.                 + "INPUT YOUR NEW PASSWORD:\n";
  10.         Scanner input = new Scanner (System.in);
  11.         System.out.println(GUISTRING);
  12.         String pass = input.nextLine();
  13.        
  14.         System.out.println("hasOnlyValidSymbols: " +
  15.                 hasOnlyValidSymbols(pass));
  16.         System.out.println("hasTwoDigits: " +
  17.                 hasTwoDigits(pass));
  18.         System.out.println("hasUppercase: " +
  19.                 hasUppercase(pass));
  20.         System.out.println("hasLowercase: " +
  21.                 hasLowercase(pass));
  22.         System.out.println("hasSymbol: " +
  23.                 hasSymbol(pass));
  24.        
  25.         if ((pass.length() >= 8 && pass.length() <= 32) &&
  26.                 hasOnlyValidSymbols(pass) && hasTwoDigits(pass)
  27.                 && hasUppercase(pass) && hasLowercase(pass)
  28.                 && hasSymbol(pass)) oKayPass = true;
  29.        
  30.         if (oKayPass)
  31.         {
  32.             System.out.println("Your password is good!");
  33.         }
  34.         else
  35.         {
  36.             System.out.println("Your password is bad :(");
  37.         }
  38.        
  39.         input.close();
  40.     }
  41.    
  42.     public static boolean hasOnlyValidSymbols (String password)
  43.     {
  44.         return password.matches("^[A-Za-z0-9@#$%]*$");
  45.     }
  46.     public static boolean hasTwoDigits (String password)
  47.     {
  48.         int numberOfDigits = 0;
  49.         for (int i = 0; i <= password.length() - 1; i++)
  50.         {
  51.             if (Character.isDigit(password.charAt(i))) numberOfDigits++;
  52.         }
  53.         return numberOfDigits >= 2;
  54.     }
  55.     public static boolean hasUppercase (String password)
  56.     {
  57.         boolean doesIt = false;
  58.         String[] uppers = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
  59.         for (int i = 0; i <= uppers.length - 1; i++)
  60.         {
  61.             if (password.contains(uppers[i])) doesIt = true;
  62.         }
  63.         return doesIt;
  64.     }
  65.     public static boolean hasLowercase (String password)
  66.     {
  67.         boolean doesIt = false;
  68.         String[] lowers = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
  69.         for (int i = 0; i <= lowers.length - 1; i++)
  70.         {
  71.             if (password.contains(lowers[i])) doesIt = true;
  72.         }
  73.         return doesIt;
  74.     }
  75.     public static boolean hasSymbol (String password)
  76.     {
  77.         boolean doesIt = false;
  78.         String[] symbols = {"@", "#", "$", "%"};
  79.         for (int i = 0; i <= symbols.length - 1; i++)
  80.         {
  81.             if (password.contains(symbols[i])) doesIt = true;
  82.         }
  83.         return doesIt;
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement