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
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;
}
}
Comments
Leave a comment