Okay, so for my coding project i need to complete the infamous Vowels-R-Us program. I am having trouble with a logic error.
This problem-code is specified inside the comment titled "BROKEN CODE"
In this project, Vowels are defined as A S C or L and all other letters are considered consonants.
the test date or word given is "PDAE"
What i am trying to do in this method is detect whether the end of my string, word, ends in a consonant or a vowel or two consonants/vowels. Based on whether it ends in one or two, it sets wordEnd to its respective value:
private static void checkEnd() {
This checks if the last letter is a vowel, if true, sets wordEnd to 1:
if ("A".equals(word.substring(word.length())) || "C".equals(word.substring(word.length())) || "S".equals(word.substring(word.length())) || "L".equals(word.substring(word.length()))) {
wordEnd = 2;
This will check to see if the second to last word is a vowel as well, then sets wordEnd to 3:
if ("A".equals(word.substring(word.length() - 1)) || "C".equals(word.substring(word.length() - 1)) || "S".equals(word.substring(word.length() - 1)) || "L".equals(word.substring(word.length() - 1))) {
wordEnd = 3;
}
}
This sets wordEnd to 2, assuming the first if statement output's false:
else {
wordEnd = 3;
THIS IS THE BROKEN CODE I THINK:
if ("A" != (word.substring(word.length() - 1)) && "C" != (word.substring(word.length() - 1)) && "S" != (word.substring(word.length() - 1)) && "L" != (word.substring(word.length() - 1)))
wordEnd = 1;
The code should theoretically output false and leave the value of wordEnd at 1, but it comes out as true and sets it to 3..
I need an alternative to using != to compare the strings, what can i do for that?
This just prints the variable wordEnd, but always comes out to 3 for some reason...:
/*
* TEST CODE
*/
System.out.println(wordEnd);
}
}
MAJOR LOGIC EDIT
The last two statements. Else, then if:
i had to swap 1 and 3 because of a logic error. if the last if outputs false, then it should be outputting 1, for the last two not being two consonants. So my bad, but thanks to @clcto for helping me with my other error when not using !x.equals
Sorry for the continuous errors, i have only been coding for a couple months.
Negate the result of the
equals()
call: