Hello everyone, I am so close to achieving my goal with this program. I just can't seam to figure out where the for loop goes wrong and "mis-counts" the amount of consonants and vowels. Also, in the else...if statement if anyone has advise to make it so it excludes all characters (like !, -, ...etc) besides vowels that would help so much! Part of the consonant problem I believe is it is counting spaces and other characters maybe?
Thanks so much in advance for any help!
import java.lang.String; public class StringProcessor { String string; int vowels; int consonants; public void Count() { vowels = 0; consonants = 0; for(int i = 0; i < string.length(); i++) { char c = string.charAt(i); if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') vowels++; else if(c != 'a' || c != 'e' || c != 'i' || c != 'o' || c != 'u' || c != ' ') consonants++; } } public void display() { System.out.println(string + " has " + vowels + " vowels and " + consonants + " consonants."); } public StringProcessor(String aString) { string = aString; } public void setString(String stString) { string = stString; } } public class TestProcessor { public static void main(String[] args) { StringProcessor processor = new StringProcessor("Java Rules"); processor.Count(); processor.display(); processor.setString("I like on-line classes"); processor.Count(); processor.display(); processor.setString("Spring break was AWESOME!"); processor.Count(); processor.display(); } }
The output is supposed to look like this:
"Java Rules" has 4 vowels and 5 consonants
"I like on-line classes" has 8 vowels and 10 consonants
"Spring break was AWESOME!" has 8 vowels and 13 consonants
And I get this:
Java Rules has 4 vowels and 6 consonants.
I like on-line classes has 7 vowels and 15 consonants.
Spring break was AWESOME! has 4 vowels and 21 consonants.
Thanks again!