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?
4
u/[deleted] Feb 21 '19 edited Feb 21 '19
Is this what you are looking for? Let me know if it does not make sense. I would be happy to explain. You should also note that a switch statement would be a much better option over else if.
EDIT: If /u/Dusty1997 has it right, then my above code is not what you are looking for. You would need some sort of stop code so that the user can indicate when they want to terminate the process. In that case, you could just put an if statement in the do block above looking for that specific stop code, and then either break out or change the value of z to text.length() + 1 to make the while condition falsy, assuming the stop code is a char not a string, for you are iterating char by char.