w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
In Java, how to create a simple program that prints the number of consonants and vowels in a phrase?

contains searches for the entire String, not the individual letters.

The simplest way to do this, from the top of my head assuming there's no magical String method I'm missing, would be to manually check each character.

You should convert the entire string to upper case using toUpperCase and then check if the character is a vowel AEIOU.

if(string.charAt(i) == 'A' || ... /* and so on */) {
    countVow++;
}
else {
    countCons++;
}

If it is, add 1 to vowels. Else, add 1 to consonants. It's either a vowel or a consonant, so if you just check those five characters, you know what it is.

Since this is probably a homework problem, I've provided you a step in the right direction. You should work towards the solution and come back if you need help.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.