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