3

When my code compiles it is giving me multiple outputs of my vowels. Almost like its giving me an output after it checks each character. how do i make it give just one output with all my vowels and others. in 1 output??

Please help...

import java.util.*;

public class Lowercasevowelcounter {

    public static void main(String[] args) {

    int a=0, e=0, x=0;
    int u=0, o=0, other=0;

    String text;
    Scanner sc = new Scanner(System.in);


    System.out.println("Enter a string of words : ");
    text = sc.nextLine();

    for (int i = 0; i < text.length(); i++){
        char c = text.charAt(i);

        if (c == 'a') a++;

        else if (c == 'e')  
          e++;

        else if (c == 'i')  
          x++;

        else if (c == 'o')
          o++;

        else if (c == 'u')
          u++;

        else 
          other++;

        System.out.println("a: " + a + "\n" +
                "e: " + e + "\n" +
                "i: " + x + "\n" +
                "o: " + o + "\n" +
                "u: " + u + "\n" +
                "other: " + other);
    }
  }
}
Savage99
  • 59
  • 8

1 Answers1

3

You have the System.out.println(...) inside of the for loop. Try extracting it to be outside of the for loop.

...
for (int i = 0; i < text.length(); i++){
    char c = text.charAt(i);

    if (c == 'a')a++;
        else if (c == 'e')  e++;
        else if (c == 'i')  x++;
        else if (c == 'o')  o++;
        else if (c == 'u')  u++;
    else 
    other++;
}

System.out.println("a: " + a + "\n" +
        "e: " + e + "\n" +
        "i: " + x + "\n" +
        "o: " + o + "\n" +
        "u: " + u + "\n" +
        "other: " + other);
...
Jesse Buss
  • 833
  • 6
  • 13