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

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.

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

       int z = 0;  

       do {  
             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++;  

             z++;  
       } while (z < text.length());  


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

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.

1

u/Darknight03 Feb 21 '19

This is not I was looking for, however it was very helpful thank you so much! By repeating I meant that the user repeatedly can type in a word and get the vowels. How would I go about that?

3

u/Dusty1997 Feb 21 '19

Also, I would suggest a switch statement instead of the repeated else if, (it's tidier and more appreciated by other programmers).

Switch(text){ case 'a': a++; Break; Case 'b': . . . default: break; } Break exits the switch block, default is for cases you don't want to include ('b', 'c' etc...)

Here's the oracle docs for it:

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

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.

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

1

u/NatKurien Feb 21 '19

I think you need to be a little more specific with regard to your requirement. What is the condition that you are looking to loop through?