Vowel Counter Program

D

David

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);
}

}
 
J

Jim

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
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

David said:
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.
 
T

Tr0mBoNe-

Jim said:
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
 
R

Roedy Green

// Increment vowel counters

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.
 
P

Phil Earnhardt

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
 
R

Russell Gold

Roedy Green said:
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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top