Answer to Question #313733 in Java | JSP | JSF for sowmya

Question #313733

In java


Create a java program to test if a password is valid or not. For the password to be acceptable it must satisfy the following rules:

- At least one (1) vowel.

- No two (2) consecutive same letter, except "ee" or "oo".

- No three (3) consecutive vowels.

- Must not be more than 20 characters and no uppercase letter.

Input

Enter one or more possible password per line, the word "end" ends the input.

Output

Display the entered password followed by either "is Valid" or "is Invalid"

Sample Input

bee

books

pc

beautiful

ambitious

end

Sample Output

bee is Valid

books is Valid

pc is Invalid

beautiful is Invalid

ambitious is Invalid


1
Expert's answer
2022-03-18T07:50:12-0400


import java.util.Scanner;


public class App {


	/** Main Method */
	public static void main(String[] args) {
		Scanner keyBoard = new Scanner(System.in); // Create a Scanner
		String word = "";
		String[] password = new String[1000];
		int counter = 0;
		System.out.println("Enter one or more possible password per line, the word \"end\" ends the input: ");
		while (word.compareToIgnoreCase("end") != 0) {
			word = keyBoard.nextLine();
			if (word.compareToIgnoreCase("end") != 0) {
				password[counter] = word;
				counter++;
			}
		}
		for (int i = 0; i < counter; i++) {
			if (isValid(password[i])) {
				System.out.println(password[i] + " is Valid");
			} else {
				System.out.println(password[i] + " is Invalid");
			}


		}
		keyBoard.close();
	}


	/****
	 * - At least one (1) vowel. - No two (2) consecutive same letter, except "ee"
	 * or "oo". - No three (3) consecutive vowels. - Must not be more than 20
	 * characters and no uppercase letter.
	 * 
	 * @param password
	 * @return
	 */
	public static boolean isValid(String password) {
		final int LENGTH_OF_VALID_PASSWORD = 20; // Valid length of password


		boolean validPassword = isLengthValid(password, LENGTH_OF_VALID_PASSWORD) && hasAtLeastOneVowel(password)
				&& !containsUpperCaseCharacter(password) && !hasThreeConsecutiveVowels(password);


		return validPassword;
	}


	public static boolean isLengthValid(String password, int validLength) {
		return password.length() <= validLength;
	}


	public static boolean hasAtLeastOneVowel(String password) {
		password = password.toLowerCase();
		for (int i = 0; i < password.length(); i++) {
			if (isVowel(password.charAt(i))) {
				return true;
			}
		}
		return false;
	}


	static boolean isVowel(char letter) {
		if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u') {
			return true;
		}
		return false;
	}


	public static boolean consecutiveSameLetter(String password) {
		password = password.toLowerCase();
		for (int i = 0; i < password.length() - 1; i++) {
			if ((password.toLowerCase().charAt(i) == 'e' && password.toLowerCase().charAt(i + 1) == 'e')
					|| (password.toLowerCase().charAt(i) == 'o' && password.toLowerCase().charAt(i + 1) == 'o')) {
				continue;
			}
			if (password.toLowerCase().charAt(i) == password.toLowerCase().charAt(i + 1)) {
				return false;
			}
		}
		return true;
	}


	static boolean hasThreeConsecutiveVowels(String password) {
		password = password.toLowerCase();
		for (int i = 0; i < password.length() - 2; i++) {
			if (isVowel(password.charAt(i)) && isVowel(password.charAt(i + 1)) && isVowel(password.charAt(i + 2))) {
				return true;
			}
		}
		return false;
	}


	static boolean containsUpperCaseCharacter(String password) {
		for (int i = 0; i < password.length(); i++) {
			if (Character.isUpperCase(password.charAt(i))) {
				return true;
			}
		}


		return false;
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS