Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Exc4PasswordValidator {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- if(getPasswordCharacters(input) && checkLettersDigits(input)&& checkTwoDigits(input)){
- System.out.println("Password is valid");
- }
- if (!getPasswordCharacters(input)) {
- System.out.println("Password must be between 6 and 10 characters");
- }
- if (!checkLettersDigits(input)){
- System.out.println("Password must consist only of letters and digits");
- }
- if (!checkTwoDigits(input)){
- System.out.println("Password must have at least 2 digits");
- }
- }
- private static boolean getPasswordCharacters(String input) {
- return input.length() >= 6 && input.length() <= 10;
- }
- private static boolean checkLettersDigits(String input){
- for (int i = 0; i < input.length(); i++) {
- char index = input.charAt(i);
- boolean digit = Character.isDigit(index);
- boolean symbol = Character.isLetter(index);
- if (!digit && !symbol){
- return false;
- }
- }
- return true;
- }
- private static boolean checkTwoDigits(String input) {
- int count = 0;
- for (int i = 0; i <input.length() ; i++) {
- char index = input.charAt(i);
- if (Character.isDigit(index)){
- count++;
- }
- if (count == 2){
- return true;
- }
- }
- return false;
- }
- }
- /*Write a program that checks if a given password is valid. Password rules are:
- • 6 – 10 characters (inclusive);
- • Consists only of letters and digits;
- • Have at least 2 digits.
- If a password is valid, print "Password is valid". If it is not valid,
- for every unfulfilled rule print a message:
- • "Password must be between 6 and 10 characters";
- • "Password must consist only of letters and digits";
- • "Password must have at least 2 digits".
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement