Return Each Vowels

public class one
{
    public static void main(String [] args)
    {       
    }

    public static int vowels( String s )
    {
        int countA = 0; int countE = 0; int countI = 0; 
        int countO = 0; int countU = 0;
        for(int i = 0; i < s.length(); i++)
        {
            if(s.charAt(i) == 'a')
            {
                countA++;
            }
            else if(s.charAt(i) == 'e')
            {
                countE++;
            }
            else if(s.charAt(i) == 'i')
            {
                countI++;
            }
            else if(s.charAt(i) == 'o')
            {
                countO++;
            }
            else if(s.charAt(i) == 'u')
            {
                countU++;
            }
        }
        return countA; return countE; return countI;
        return countO; return countU;
    }
}

The program I'am making will count the number of vowels in a string. I'am trying to get a way to apply it to main and output the result. I tried doing different returns like below but its not working..

Jon Skeet
people
quotationmark

You can only return a single value from a method call. However, that value can be an array or a collection.

Two options to consider:

  • Change your method to accept a second parameter of "the character to count" occurrences of, and call it 5 times.
  • Change your method to return an array, where element 0 is the count of As, element 1 is the count of Es etc

The first approach is cleaner and more reusable (IMO) but less efficient. You'd have something like:

String text = ...;
System.out.println("The vowel a appears " + count(text, 'a') + " times.");
System.out.println("The vowel e appears " + count(text, 'e') + " times.");
System.out.println("The vowel i appears " + count(text, 'i') + " times.");
System.out.println("The vowel o appears " + count(text, 'o') + " times.");
System.out.println("The vowel u appears " + count(text, 'u') + " times.");

...

private static int count(String text, char charToCount) {
    int count = 0;
    for (int i = 0; i < text.length(); i++) {
        if (text.charAt(i) == charToCount) {
            count++;
        }
    }
    return count;
}

people

See more on this question at Stackoverflow