Opache Opache - 4 months ago 22
Java Question

Return a boolean if the first letter is a consonant or not

I've got a program that I am writing, it is suppose to translate words to pig latin (really basic exercise)
I can get the consonants to change to pig latin with no issue but the other side is that I need to check if the first letter is a consonant and if it's not then the translation changes.

I've built a method that checks if the first letter of a word is a consonant and if it is return a boolean as true otherwise false.

For some reason the method won't run the "else" statement

Here is the method that was written.

private static boolean firstLetterConsonant(String s)
boolean isConsonant;

//check to see if the first letter is not a vowel
if(s != "a" && s != "e" && s != "i" && s != "o" && s != "u")
isConsonant = true;
} else {
isConsonant = false;
return isConsonant;

As a side comment the method is getting passed the string that was entered by the user elsewhere in the program.


You have 3 options.

  1. Use String#startsWith() and an if condition checking for 5 vowels (just like what you are doing)

  2. Take the first char out of the String using String#charAt() and check if it is a vowel (and return false if that's the case).

  3. Use String#matches and use "(?i)[^aeiou].*"