Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PasswordDriver
- {
- public static void main(String[] args)
- {
- //Initiera variablerna.
- boolean oKayPass = false;
- String GUISTRING = "***WECLOME TO PASSWORD CHECKER***\n"
- + "INPUT YOUR NEW PASSWORD:\n";
- Scanner input = new Scanner (System.in);
- System.out.println(GUISTRING);
- //Get input.
- String pass = input.nextLine();
- //Debug.
- System.out.println("hasOnlyValidSymbols: " +
- hasOnlyValidSymbols(pass));
- System.out.println("hasTwoDigits: " +
- hasTwoDigits(pass));
- System.out.println("hasUppercase: " +
- hasUppercase(pass));
- System.out.println("hasLowercase: " +
- hasLowercase(pass));
- System.out.println("hasSymbol: " +
- hasSymbol(pass));
- //Does it meet requirements?
- if ((pass.length() >= 8 && pass.length() <= 32) &&
- hasOnlyValidSymbols(pass) && hasTwoDigits(pass)
- && hasUppercase(pass) && hasLowercase(pass)
- && hasSymbol(pass)) oKayPass = true;
- //Let the user know the result.
- if (oKayPass) System.out.println("Your password is good!");
- else System.out.println("Your password is bad :(");
- //Close the reader.
- input.close();
- }
- public static boolean hasOnlyValidSymbols (String password)
- {
- //Regex, tests to make sure we only have letters, numbers, or @#$%.
- return password.matches("^[A-Za-z0-9@#$%]*$");
- }
- public static boolean hasTwoDigits (String password)
- {
- //Counts how many digits we have. Two or more, we're good to go.
- int numberOfDigits = 0;
- for (int i = 0; i <= password.length() - 1; i++)
- if (Character.isDigit(password.charAt(i))) numberOfDigits++;
- return numberOfDigits >= 2;
- }
- public static boolean hasUppercase (String password)
- {
- //Cycle through an array of uppercase letters, checking to make sure one of the
- //characters in the password match it.
- 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"};
- for (int i = 0; i <= uppers.length - 1; i++)
- if (password.contains(uppers[i])) return true;
- return false;
- }
- public static boolean hasLowercase (String password)
- {
- //Cycle through an array of lowercase letters, checking to make sure one of the
- //characters in the password match it.
- 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"};
- for (int i = 0; i <= lowers.length - 1; i++)
- if (password.contains(lowers[i])) return true;
- return false;
- }
- public static boolean hasSymbol (String password)
- {
- //Cycle through an array of symbols, checking to make sure one of the
- //characters in the password match it.
- String[] symbols = {"@", "#", "$", "%"};
- for (int i = 0; i <= symbols.length - 1; i++)
- if (password.contains(symbols[i])) return true;
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement