r/javahelp • • 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?

1 Upvotes

6 comments sorted by

View all comments

2

u/DeepanRajV Feb 21 '19

is this what you are looking for

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);
    do {
        System.out.print("Enter a string of characters: ");
        text = scan.nextLine();
        for (int z = 0; z < text.length(); z++)
            switch (text.charAt(z)) {
                case 'a':
                    a++;
                    break;
                case 'e':
                    e++;
                    break;
                case 'i':
                    i++;
                    break;
                case 'o':
                    o++;
                    break;
                case 'u':
                    u++;
                    break;
                default:
                    other++;
                    break;
            }
        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);
    } while (!text.equalsIgnoreCase("stop"));
}