This one I almost have figured out (I believe). it is a program that takes a String in the main class and calls on a method to count the vowels inside the String. I then have to count each vowel (a,e,i,o,u) and input the total number of vowels (separately) into an array and return that array such that the output would be {1, 2, 1, 3, 1} or whatever it may be...any help?

public class VowelCount
{
    public static void main(String[] args)
        {
            String VC = "black banana republic boots";
            System.out.println(vowelCount(VC)); 
            
        }
        public static int[] vowelCount (String list)
        {
            int x = list.length();
            int[] aeiou = new int[4];
                                                     
            if(list.substring(0, x).equals("a"))
                aeiou[0]++;
            
            else if(list.substring(0, x).equals("e"))
                aeiou[1]++;

            else if(list.substring (0, x).equals("i"))
                 aeiou[2]++;

            else if(list.substring (0, x).equals("o"))
                 aeiou[3]++;
                     
            else if(list.substring(0, x).equals("u"))
                  aeiou[4]++;
                                
            return aeiou;
        }
    }

Dani AI

Generated

— good start; was on the right track. Two quick issues in the posted snippet: the counts array needs five slots (one per a,e,i,o,u) and you must examine each character of the string (comparing the whole string to "a" will never match). Below are compact, correct examples and a few practical tips.

import java.util.Arrays;

public class VowelCount {
    public static void main(String[] args) {
        String s = "black banana republic boots";
        System.out.println(Arrays.toString(vowelCount(s))); // prints [4, 1, 1, 2, 1]
    }

    public static int[] vowelCount(String s) {
        int[] counts = new int[5];           // order: a, e, i, o, u
        String vowels = "aeiou";
        for (int i = 0; i < s.length(); i++) {
            int idx = vowels.indexOf(Character.toLowerCase(s.charAt(i)));
            if (idx >= 0) counts[idx]++;
        }
        return counts;
    }
}
def vowel_counts(s):
    vowels = "aeiou"
    counts = [0] * 5
    for ch in s.lower():
        i = vowels.find(ch)
        if i != -1:
            counts[i] += 1
    return counts

print(vowel_counts("black banana republic boots"))  # [4, 1, 1, 2, 1]

Notes and quick checks:

  • Expected result for the sample string is [4, 1, 1, 2, 1] (a,e,i,o,u).
  • In Java, use Arrays.toString(...) to print an int[]; printing the array object directly shows a reference.
  • Make the routine case-insensitive (shown above). If you must handle accented vowels (á, é, etc.), normalize Unicode first.
  • Complexity is O(n); test with empty and null strings and with strings containing digits/punctuation.

Recommended Answers

All 3 Replies

Use a for loop in you "vowelCount". Also, you can't print an array like that, use a for loop for that too. Look over the usage of "substring" again.

will they be nested for loops? I'm thinking not, but I don't know much about this stuff. and I will have to use a compound if statement, right? tryin to figure it out

Admin-You can delete this post. I figured it out
thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.