Difference between revisions of "Vowel Counter"

From CompSciWiki
Jump to: navigation, search
m
Line 8: Line 8:
 
[[Image:Wiki_array02.jpg|center]]<BR>
 
[[Image:Wiki_array02.jpg|center]]<BR>
  
|Solution=I still have to do the solution write-up...
+
|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.<br /><br />
 +
 
 +
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).<br /><br />
 +
 
 +
For starters, you'll need your variables.  You should only really need three main ones: 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.
 +
 
 +
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:
 +
<pre>
 +
if( input == null || input.equals( "" ) )
 +
{
 +
    //user canceled, or gave no input -- we are done
 +
    done = true;
 +
}
 +
else
 +
{
 +
    (...)
 +
}
 +
</pre>
  
 
|SolutionCode=
 
|SolutionCode=
Line 38: Line 55:
 
                 for( int i = 0; i < input.length(); i++ )
 
                 for( int i = 0; i < input.length(); i++ )
 
                 {
 
                 {
                char letter = input.charAt( i );
+
                    //get the character at position i in the input string
 +
                    char letter = input.charAt( i );
 
                
 
                
 
                     //check if it's a vowel
 
                     //check if it's a vowel

Revision as of 21:50, 11 April 2010

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, you'll need your variables. You should only really need three main ones: 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.

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:

if( input == null || input.equals( "" ) )
{
    //user canceled, or gave no input -- we are done
    done = true;
}
else
{
    (...)
}

Code

Solution Code

Back to the Program-A-Day homepage