r/javahelp • u/Darknight03 • Feb 21 '19
Need help with Java Code!!!
Here is my code:
import java.util.Scanner;
public class VowelCount
{
public static void main(String args [])
{
int a = 0, e = 0, i = 0, o = 0, u = 0, other = 0;
String text;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a string of characters: ");
text = scan.nextLine();
for (int z = 0; z < text.length(); z++) {
char c = text.charAt(z);
if (c=='a')
{
a++;
}
else if( c=='e')
{
e++;
}
else if(c=='i')
{
i++;
}
else if(c=='o')
{
o++;
}
else if (c=='u')
{
u++;
}
else
other++;
}
System.out.println("Number of vowel in the string: ");
System.out.println("a: " + a);
System.out.println("e: " + e);
System.out.println("i: " + i);
System.out.println("o: " + o);
System.out.println("u: " + u);
System.out.println("Number of non-vowel characters: " + other);
}
}
The code runs perfectly, but how do you add a do while loop to repeat this process?
TL;DR: How do you add a do while loop to repeat this process?
2
u/Dusty1997 Feb 21 '19
Assuming by repeating you mean that the user repeatedly can type in a word and get the vowels. The program needs some input on when to stop, then a while loop running the vowel counter until the inputted text equals an end command. So I would suggest: String endString = "END" //or any string you want to end with //Make variables //Scan text in While (!text.equals(endString)){ //Set variables to 0 //Count vowels //Output //Scan text in }
Obviously fill in the comments as they are described (sorry I'm on my phone so can't so nice code).
E.g.
Input: Hello World END
Will print out counts for hello, then world, then it will stop.