Vowel Counter Program  
Author Message
dboyd901





PostPosted: 2003-9-21 7:51:00 Top

java-programmer, Vowel Counter Program Hey,

I am trying to write a basic program that counts how many lowercase
vowels and non-vowels are in a string. I'm trying to use the
StringTokenizer function. Also, I have to prompt the user again by
using a do while loop. The string ends with the string "DONE".
Here's my code. If anyone could help me, please do. Thanks.


import cs1.Keyboard;
import java.util.StringTokenizer;

class vowelCounter;
{
public static void main(String[] args)
{
// Initialize Answer to Zero
int answer = 1;

do // Start the do loop
{

// Declare all variables and set counters to zero
int aCounter = 0;
int eCounter = 0;
int iCounter = 0;
int oCounter = 0;
int uCounter = 0;
int nonVowelCounter = 0;

String line, temp;
int letter;

StringTokenizer tokenizer;

// Prompt the user for String and read string
System.out.print ("Please enter text (Type DONE to quit):
");
line = Keyboard.readString();

while (!line.equals("DONE"))
{
tokenizer = new StringTokenizer (line);
while (tokenizer.hasMoreTokens())
{
temp = tokenizer.nextToken();
letter = temp.length();

for (int i=0; i<=letter; i++)
{
// Increment vowel counters
if (line.charAt(i) == 'a')
aCounter++;

else if (line.charAt(i) == 'e')
eCounter++;

else if (line.charAt(i) == 'i')
iCounter++;

else if (line.charAt(i) == 'o')
oCounter++;

else if (line.charAt(i) == 'u')
uCounter++;

else
nonVowelCounter++;
}

}
line = Keyboard.readString();


}

// Print out number of vowels and non-vowels
System.out.println ("Number of 'a': " +aCounter);
System.out.println ("Number of 'e': " +eCounter);
System.out.println ("Number of 'i': " +iCounter);
System.out.println ("Number of 'o': " +oCounter);
System.out.println ("Number of 'u': " +uCounter);
System.out.println ("Number of non-vowels: "
+nonVowelCounter);

// Prompt the User About Continuing
System.out.print ("Do you want to continue?
(0=exit/1=continue) ");
answer = Keyboard.readInt();
System.out.println ("");

}while (answer == 1);
}

}
 
Jim





PostPosted: 2003-9-21 10:03:00 Top

java-programmer >> Vowel Counter Program On 20 Sep 2003 16:51:02 -0700, email***@***.com (David) wrote:

>Hey,
>
>I am trying to write a basic program that counts how many lowercase
>vowels and non-vowels are in a string. I'm trying to use the
>StringTokenizer function. Also, I have to prompt the user again by
>using a do while loop. The string ends with the string "DONE".
>Here's my code. If anyone could help me, please do. Thanks.
>
>
> import cs1.Keyboard;
> import java.util.StringTokenizer;
>
> class vowelCounter;
> {
> public static void main(String[] args)
> {
> // Initialize Answer to Zero
> int answer = 1;
>
> do // Start the do loop
> {
>
> // Declare all variables and set counters to zero
> int aCounter = 0;
> int eCounter = 0;
> int iCounter = 0;
> int oCounter = 0;
> int uCounter = 0;
> int nonVowelCounter = 0;
>
> String line, temp;
> int letter;
>
> StringTokenizer tokenizer;
>
> // Prompt the user for String and read string
> System.out.print ("Please enter text (Type DONE to quit):
>");
> line = Keyboard.readString();
>
> while (!line.equals("DONE"))
> {
> tokenizer = new StringTokenizer (line);
> while (tokenizer.hasMoreTokens())
> {
> temp = tokenizer.nextToken();
> letter = temp.length();
>
> for (int i=0; i<=letter; i++)
> {
> // Increment vowel counters
> if (line.charAt(i) == 'a')
> aCounter++;
>
> else if (line.charAt(i) == 'e')
> eCounter++;
>
> else if (line.charAt(i) == 'i')
> iCounter++;
>
> else if (line.charAt(i) == 'o')
> oCounter++;
>
> else if (line.charAt(i) == 'u')
> uCounter++;
>
> else
> nonVowelCounter++;
> }
>
> }
> line = Keyboard.readString();
>
>
> }
>
> // Print out number of vowels and non-vowels
> System.out.println ("Number of 'a': " +aCounter);
> System.out.println ("Number of 'e': " +eCounter);
> System.out.println ("Number of 'i': " +iCounter);
> System.out.println ("Number of 'o': " +oCounter);
> System.out.println ("Number of 'u': " +uCounter);
> System.out.println ("Number of non-vowels: "
>+nonVowelCounter);
>
> // Prompt the User About Continuing
> System.out.print ("Do you want to continue?
>(0=exit/1=continue) ");
> answer = Keyboard.readInt();
> System.out.println ("");
>
> }while (answer == 1);
> }
>
> }

First : Get rid of the StringTokenizer and just use 'line' as entered.
Second : Find out about the 'switch' statement, and
Third : Please state your problem in the form of a question.

Jim
 
Daniel Sj鯾lom





PostPosted: 2003-9-22 1:49:00 Top

java-programmer >> Vowel Counter Program David wrote:

> Hey,
>
> I am trying to write a basic program that counts how many lowercase
> vowels and non-vowels are in a string. I'm trying to use the
> StringTokenizer function. Also, I have to prompt the user again by
> using a do while loop. The string ends with the string "DONE".
> Here's my code. If anyone could help me, please do. Thanks.

Next time, specify *what* you need help with. Generic "help me" messages
tend to be ignored in newsgroups like this. But anyway, here are some
pointers:

> import cs1.Keyboard;
> import java.util.StringTokenizer;
>
> class vowelCounter;

Remove ;

> {
> public static void main(String[] args)
> {
> // Initialize Answer to Zero
> int answer = 1;
>
> do // Start the do loop
> {
>
> // Declare all variables and set counters to zero
> int aCounter = 0;
> int eCounter = 0;
> int iCounter = 0;
> int oCounter = 0;
> int uCounter = 0;
> int nonVowelCounter = 0;
>
> String line, temp;
> int letter;
>
> StringTokenizer tokenizer;

Why do you declare variables here, instead of declaring them when they
are first used?

> // Prompt the user for String and read string
> System.out.print ("Please enter text (Type DONE to quit):
> ");
> line = Keyboard.readString();
>
> while (!line.equals("DONE"))
> {
> tokenizer = new StringTokenizer (line);

There is no reason to use StringTokenizer here. A regular String will do.

> while (tokenizer.hasMoreTokens())
> {
> temp = tokenizer.nextToken();
> letter = temp.length();
>
> for (int i=0; i<=letter; i++)

This loop will always generate a StringIndexOutOfBoundsException.
Remember that String indices start with zero, and end with
String.length()-1.

> {
> // Increment vowel counters
> if (line.charAt(i) == 'a')
> aCounter++;
>
> else if (line.charAt(i) == 'e')
> eCounter++;
>
> else if (line.charAt(i) == 'i')
> iCounter++;
>
> else if (line.charAt(i) == 'o')
> oCounter++;
>
> else if (line.charAt(i) == 'u')
> uCounter++;
>
> else
> nonVowelCounter++;

Instead of the above if-else chain, use a switch.

> }
>
> }
> line = Keyboard.readString();
>
>
> }
>
> // Print out number of vowels and non-vowels
> System.out.println ("Number of 'a': " +aCounter);
> System.out.println ("Number of 'e': " +eCounter);
> System.out.println ("Number of 'i': " +iCounter);
> System.out.println ("Number of 'o': " +oCounter);
> System.out.println ("Number of 'u': " +uCounter);
> System.out.println ("Number of non-vowels: "
> +nonVowelCounter);
>
> // Prompt the User About Continuing
> System.out.print ("Do you want to continue?
> (0=exit/1=continue) ");
> answer = Keyboard.readInt();
> System.out.println ("");
>
> }while (answer == 1);
> }
>
> }

And last but not least, there is no reason to put all your code in the
main method. Try to write something more object-oriented and reusable.
If you don't know what that means, ask your professor.
--
Daniel Sj鯾lom



 
 
058147m





PostPosted: 2003-9-22 2:51:00 Top

java-programmer >> Vowel Counter Program Jim <email***@***.com> wrote in message news:<email***@***.com>...
> On 20 Sep 2003 16:51:02 -0700, email***@***.com (David) wrote:
>
> >Hey,
> >
> >I am trying to write a basic program that counts how many lowercase
> >vowels and non-vowels are in a string. I'm trying to use the
> >StringTokenizer function. Also, I have to prompt the user again by
> >using a do while loop. The string ends with the string "DONE".
> >Here's my code. If anyone could help me, please do. Thanks.
> >
> >
> > import cs1.Keyboard;
> > import java.util.StringTokenizer;
> >
> > class vowelCounter;
> > {
> > public static void main(String[] args)
> > {
> > // Initialize Answer to Zero
> > int answer = 1;
> >
> > do // Start the do loop
> > {
> >
> > // Declare all variables and set counters to zero
> > int aCounter = 0;
> > int eCounter = 0;
> > int iCounter = 0;
> > int oCounter = 0;
> > int uCounter = 0;
> > int nonVowelCounter = 0;
> >
> > String line, temp;
> > int letter;
> >
> > StringTokenizer tokenizer;
> >
> > // Prompt the user for String and read string
> > System.out.print ("Please enter text (Type DONE to quit):
> >");
> > line = Keyboard.readString();
> >
> > while (!line.equals("DONE"))
> > {
> > tokenizer = new StringTokenizer (line);
> > while (tokenizer.hasMoreTokens())
> > {
> > temp = tokenizer.nextToken();
> > letter = temp.length();
> >
> > for (int i=0; i<=letter; i++)
> > {
> > // Increment vowel counters
> > if (line.charAt(i) == 'a')
> > aCounter++;
> >
> > else if (line.charAt(i) == 'e')
> > eCounter++;
> >
> > else if (line.charAt(i) == 'i')
> > iCounter++;
> >
> > else if (line.charAt(i) == 'o')
> > oCounter++;
> >
> > else if (line.charAt(i) == 'u')
> > uCounter++;
> >
> > else
> > nonVowelCounter++;
> > }
> >
> > }
> > line = Keyboard.readString();
> >
> >
> > }
> >
> > // Print out number of vowels and non-vowels
> > System.out.println ("Number of 'a': " +aCounter);
> > System.out.println ("Number of 'e': " +eCounter);
> > System.out.println ("Number of 'i': " +iCounter);
> > System.out.println ("Number of 'o': " +oCounter);
> > System.out.println ("Number of 'u': " +uCounter);
> > System.out.println ("Number of non-vowels: "
> >+nonVowelCounter);
> >
> > // Prompt the User About Continuing
> > System.out.print ("Do you want to continue?
> >(0=exit/1=continue) ");
> > answer = Keyboard.readInt();
> > System.out.println ("");
> >
> > }while (answer == 1);
> > }
> >
> > }
>
> First : Get rid of the StringTokenizer and just use 'line' as entered.
> Second : Find out about the 'switch' statement, and
> Third : Please state your problem in the form of a question.
>
> Jim

Jim nailed that one, but if you really want to, you can just tokenize
and iterate each character into an array and search or even sort and
search the array.

andrew
 
 
dboyd901





PostPosted: 2003-9-22 5:54:00 Top

java-programmer >> Vowel Counter Program Thanks Jim, I got it working now.
 
 
Phil Earnhardt





PostPosted: 2003-9-22 12:42:00 Top

java-programmer >> Vowel Counter Program On Sun, 21 Sep 2003 21:45:29 GMT, Roedy Green <email***@***.com>
wrote:

>That hunk I would have done with a switch or like this;
>
>int vowelIndex = "aeiou".indexOf( line.charAt(i) );

Very clever. It kind of reminds me of APL code.

That line may blow the mind of the instructor who assigned this
problem.

--phil

 
 
Russell Gold





PostPosted: 2003-9-23 11:36:00 Top

java-programmer >> Vowel Counter Program In article <email***@***.com>,
Roedy Green <email***@***.com> wrote:

> That hunk I would have done with a switch or like this;
>
> int vowelIndex = "aeiou".indexOf( line.charAt(i) );
>
> vowelCount[ vowelIndex+1 ]++
>
>
> counts appear in slots 1 .. 5. Slot 0 counts consonants.

... and spaces and punctuation...

--
Russell Gold
email***@***.com
http://www.httpunit.org