Difference between revisions of "Vowel Counter"

From CompSciWiki
Jump to: navigation, search
m
Line 12: Line 12:
 
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 />
 
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.
+
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.
 
+
<pre>
 +
String input; //input string
 +
boolean done = false; //loop control variable
 +
int vowelCount = 0; //our vowel count
 +
</pre><br />
 
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:
 
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>
 
<pre>
Line 26: Line 30:
 
}
 
}
 
</pre>
 
</pre>
 +
Remember to use the String.equals( ) function, and not the '==' operator for comparing strings!<br /><br />
 +
 +
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.
  
 
|SolutionCode=
 
|SolutionCode=

Revision as of 22:04, 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, 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:

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

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.

Code

Solution Code

Back to the Program-A-Day homepage