Vowel Counter

From CompSciWiki
Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

Your task is to write a program that takes a string of input from the user, and then count the number of vowels that are contained within this string. As output, provide the user with the final count of vowels, as well as the percentage of all characters in the string that these vowels represent. Repeat this until the user either cancels the input, or enters an empty string. For the purposes of this question, you may treat 'y' as though it is always a consonant (ie, just treat 'a', 'e', 'i', 'o', and 'u' as vowels).

 

String Methods and Debugging

Wiki array02.jpg

Solution

If you did last week's Count the Alphabet program, you should already know how to figure out if a letter is a vowel. Now we just need to extrapolate from that a bit in order to apply it to this problem.

There are four different steps to this problem. First, we'll need to get the input from the user. Once we have the user's input, we'll need to process it, and then output the results. Finally, we'll need a way to repeat this process until the user is done with the program. Since you should know how to get user input and generate output using JOptionPane by now, I will not cover these steps explicitly (you can refer to Input/Output using JOptionPane if you still have trouble).

For starters, we'll need our variables. We should only really need three main ones for this program: one for holding the user's input, one for keeping track of vowels, and another one to control the while loop that we'll need.

 String input; //input string
boolean done = false; //loop control variable
int vowelCount = 0; //our vowel count 

Once you've obtained the user's input using JOptionPane, the first thing you should do is check if the user Canceled the input dialog, or if it was simply left empty. This can be achieved with an if/else structure similar to the following:

 //get input from the user
input = JOptionPane.showInputDialog( "Enter a sentence:" );

if( input == null 

Remember to use the String.equals( ) function, and not the '==' operator for comparing strings!

The bulk of our code is now going to go inside of the 'else' statement above -- this is where we'll count our vowels. Now, we could check for uppercase and lowercase vowels in our string (ie, 'a', 'A', 'e', 'E', ...), or we could simply convert our input entirely to lowercase (uppercase would work too). This allows us to only check the lowercase variants and saves us a bit of time. Once this is done, it just becomes a matter of checking the string for vowels one letter at a time, and keeping track of how many vowels we've seen thus far.

 input = input.toLowerCase();
for( int i = 0; i < input.length(); i++ )
{
    //get the character at position i in the input string
    char letter = input.charAt( i );
    
    //check if it's a vowel
    if( letter == 'a' 

Now all that's left is to output the results, and wrap our code in a while loop using our 'done' variable.

 while( !done )
{
    (...)
    //output results
    JOptionPane.showMessageDialog( null, "Vowel count: " + vowelCount + "\nVowel percentage: " + 
        vowelCount * 100 / input.length() + "%", "Vowel Counter", JOptionPane.INFORMATION_MESSAGE );
} 

And we're done! You should now have a fully functional vowel-counting program. See below for the full solution source code.

Code

Solution Code

Back to the Program-A-Day homepage