Advertisement
DigiDuncan

PasswordChecker 1.1

Nov 18th, 2015
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PasswordDriver
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         //Initiera variablerna.
  8.         boolean oKayPass = false;
  9.         String GUISTRING = "***WECLOME TO PASSWORD CHECKER***\n"
  10.                 + "INPUT YOUR NEW PASSWORD:\n";
  11.         Scanner input = new Scanner (System.in);
  12.         System.out.println(GUISTRING);
  13.        
  14.         //Get input.
  15.         String pass = input.nextLine();
  16.        
  17.         //Debug.
  18.         System.out.println("hasOnlyValidSymbols: " +
  19.                 hasOnlyValidSymbols(pass));
  20.         System.out.println("hasTwoDigits: " +
  21.                 hasTwoDigits(pass));
  22.         System.out.println("hasUppercase: " +
  23.                 hasUppercase(pass));
  24.         System.out.println("hasLowercase: " +
  25.                 hasLowercase(pass));
  26.         System.out.println("hasSymbol: " +
  27.                 hasSymbol(pass));
  28.        
  29.         //Does it meet requirements?
  30.         if ((pass.length() >= 8 && pass.length() <= 32) &&
  31.                 hasOnlyValidSymbols(pass) && hasTwoDigits(pass)
  32.                 && hasUppercase(pass) && hasLowercase(pass)
  33.                 && hasSymbol(pass)) oKayPass = true;
  34.        
  35.         //Let the user know the result.
  36.         if (oKayPass) System.out.println("Your password is good!");
  37.         else System.out.println("Your password is bad :(");
  38.        
  39.         //Close the reader.
  40.         input.close();
  41.     }
  42.    
  43.     public static boolean hasOnlyValidSymbols (String password)
  44.     {
  45.         //Regex, tests to make sure we only have letters, numbers, or @#$%.
  46.         return password.matches("^[A-Za-z0-9@#$%]*$");
  47.     }
  48.     public static boolean hasTwoDigits (String password)
  49.     {
  50.         //Counts how many digits we have. Two or more, we're good to go.
  51.         int numberOfDigits = 0;
  52.         for (int i = 0; i <= password.length() - 1; i++)
  53.             if (Character.isDigit(password.charAt(i))) numberOfDigits++;
  54.         return numberOfDigits >= 2;
  55.     }
  56.     public static boolean hasUppercase (String password)
  57.     {
  58.         //Cycle through an array of uppercase letters, checking to make sure one of the
  59.         //characters in the password match it.
  60.         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"};
  61.         for (int i = 0; i <= uppers.length - 1; i++)
  62.             if (password.contains(uppers[i])) return true;
  63.         return false;
  64.     }
  65.     public static boolean hasLowercase (String password)
  66.     {
  67.         //Cycle through an array of lowercase letters, checking to make sure one of the
  68.         //characters in the password match it.
  69.         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"};
  70.         for (int i = 0; i <= lowers.length - 1; i++)
  71.             if (password.contains(lowers[i])) return true;
  72.         return false;
  73.     }
  74.     public static boolean hasSymbol (String password)
  75.     {
  76.         //Cycle through an array of symbols, checking to make sure one of the
  77.         //characters in the password match it.
  78.         String[] symbols = {"@", "#", "$", "%"};
  79.         for (int i = 0; i <= symbols.length - 1; i++)
  80.             if (password.contains(symbols[i])) return true;
  81.         return false;
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement