I feel like my code is too messy-Im looking for possible way to make it more readable/shorter.
Goals:
1)Get three strings from user input. They have to be the same length.
a)First must contain only lowercase letters.
b)Second must contain only uppercase letters.
c)Third must have an even length and the same number of vowels and consonants.
Example1:
Input1: aaaa
Input2: BBBB
Input3: baba
2)Add to the first string all vowels from the third word (at the end of the word).
Example2:
Output: aaaaaa
3)Add to the second string all consonants from the third word (at the beginning of the word).
Example3:
Output:bbBBBB
4)Print the words:
a)with the most vowels.
b)with the most consonants.
I was thinking about ternary operator in "print the word with most vowels/consonants" phase but I failed.
Thank you for every advice.
import java.util.Scanner;
public class Main {
public static int countVowels(String str) {
str.toLowerCase();
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
|| str.charAt(i) == 'o' || str.charAt(i) == 'u' || str.charAt(i) == 'y') {
count++;
}
}
return count;
}
public static int countConsonants(String str) {
str.toLowerCase();
String stringWithoutVowels = str.replaceAll("[aeiouyAEIOUY]", "");
int numberOfConsonants = stringWithoutVowels.length();
return numberOfConsonants;
}
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (true) {
System.out.println("Provide 3 strings");
String first = scn.next();
String second = scn.next();
String third = scn.next();
String thirdWithoutVowels = third.replaceAll("[aeiouyAEIOUY]", "");
String thirdWithoutConsonants = third.replaceAll("[BCDFGHJKLMNPQRSTVXZbcdfghjklmnpqrstvxz]", "");
StringBuffer firstForAdding = new StringBuffer(first);
StringBuffer thirdForAdding = new StringBuffer(thirdWithoutVowels);
String firstRegex = "[a-z]+";
String secondRegex = "[A-Z]+";
if (third.length() % 2 == 0) {
if (thirdWithoutVowels.length() == thirdWithoutConsonants.length())
if ((first.matches(firstRegex) == true) &&
(second.matches(secondRegex)) == true) {
if (first.length() == second.length() && second.length() == third.length()) {
System.out.println("__________________________1.First with vowels from the third one(at the end).__________________________");
System.out.println(firstForAdding.append(thirdWithoutConsonants));
System.out.println("__________________________2.Second with consonants from the third one(at the begining).__________________________");
System.out.println(thirdForAdding.append(second));
if (countVowels(first) > countVowels(second) && countVowels(first) > countVowels(third)) {
System.out.println("Word with most vowels: " + first);
} else if (countVowels(second) > countVowels(first) && countVowels(second) > countVowels(third)) {
System.out.println("Word with most vowels: " + second);
} else {
System.out.println("Word with most vowels: " + third);
}
if (countConsonants(first) > countConsonants(second) && countConsonants(first) > countConsonants(third)) {
System.out.println("Word with most consonants: " + first);
} else if (countConsonants(second) > countConsonants(first) && countConsonants(second) > countConsonants(third)) {
System.out.println("Word with most consonants: " + second);
} else {
System.out.println("Word with most consonants: " + third);
}
break;
}
}
} else {
System.out.println("Wrong");
}
}
}
}