Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

write a method in java that takes an array of Strings as an argument. Each element of the argument array will contain a single word.The method must return an array of strings that contains only the words from the argument array that start with a vowel A, E, I, O, or, U).

This is what I have so far:

import java.util.Scanner;
import java.util.Arrays;    
public class VowelMethod {
public static String[]  beginsWithVowel (String   [] args  ) {
String vowel [] = new String[10];  

{

}
int count=0;
for(int i=0; i< count; i++) {  
if  ( vowel.startsWith ("a") || vowel.startsWith ("e") || vowel.startsWith ("i") ||      vowel.startsWith ("o") || s.startsWith ("u") );
return vowel;

//My error with this is that the startsWith method is only defined for String type, and not the  String[] type. 
share|improve this question
3  
You need to call startsWith on each individual string. Like so: vowel[0], vowel[1] and so on. In a loop. (Use a variable instead of 0 and 1). And set count = vowel.length so you loop over the entire array. –  keyser Nov 18 '13 at 21:45
    
To store the Strings that pass your condition use a ArrayList instead of an array as you don't know previously how many strings will start with a vowel. –  Alex Nov 18 '13 at 21:48
    
Well, you know it's <= args.length. –  SimplyPanda Nov 18 '13 at 21:49
    
@Alex and the instructions state Each element of the argument array will contain a single word.The method must return an array of strings so his function signature is OK. –  fvu Nov 18 '13 at 21:51
    
I don't know exactly what you mean. First you check the strings one by one and all your String objects that pass the vowel condition are put in an ArrayList. Once your for loop has finished, you can transform the ArrayList to a String[] and return it. Otherwise you'll be returning a String[] array with null elements. That's what I meant in case it was not clear. –  Alex Nov 18 '13 at 22:32
add comment

2 Answers

Here are some pointers:

Since you want to check each element of the array you need to access each element one by one. You do this in a loop. The number of loops is the number of elements. For each iteration, check if the current element starts with a vowel. If it does, add it to a separate array which you will return at the end of the function. To access the elements of an array you use brackets and indexes. For example, args[0] refers to the first element of the array args. You use the same syntax to insert values into arrays:

vowel[i] = args[i] would insert the element from index i into vowel at index i.

You want to loop over args and insert matches into vowel.

You could use a dynamic array such as an ArrayList, which grows in size automatically, to store the matched words (instead of vowel). This is certainly not necessary.

Remember to loop i times, where i is the size of the array.

You could use a set instead of a big if-statement, but that's not necessary either.

share|improve this answer
    
I had typed out a solution, but I prefer your answer more. One more pointer for the asker: every time the if statement triggers true, you need to do something with the string you just tested. –  SimplyPanda Nov 18 '13 at 22:03
    
Nice to hear :p I mentioned the if-trigger part, but maybe I wasn't clear. –  keyser Nov 18 '13 at 22:04
1  
Bollocks, my reading comprehension is awful. In my defense, it is a glaring error in the asker's code that needs pointing out twice! –  SimplyPanda Nov 18 '13 at 22:05
    
so I did have a for loop in my code. if I make count= array.length, and loop through it with i. but how do I add the elements with vowels to a seperate array. can i just return the array, or do I have to use the append method? –  user3006421 Nov 19 '13 at 0:11
    
I'll update the answer. It's vowel[i] = args[i] for example. Similar to simple assignment such as x = 1 if you think of [] as accessors allowing you to fetch the actual elements. –  keyser Nov 19 '13 at 7:21
add comment

A sample code, using ArrayList (as mentioned already by Keyser):

public static final String vowelsRegex = "^[aeiou].*";
public static String[] beginsWithVowel(String[] args) {
    List<String> vowelled = new ArrayList<String>();
    for (String s : args)
            if (s.toLowerCase().matches(vowelsRegex)) vowelled.add(s);
    return vowelled.toArray(new String[vowelled.size()]);
}

I'd like to point out that in these cases, it is almost always a better idea to use a regular expression instead of multiple logical operations. Hope this helps.

share|improve this answer
1  
The problem with this answer is that you just did OP's homework. Nice solution though. I prefer to avoid regexes when possible though, so I would've gone with set.contains –  keyser Nov 18 '13 at 22:07
    
Ugh (on two counts) ... first, I should have realized this to be a homework question instead of mindlessly providing a code; and second, set.contains() is so much more elegant for this problem! –  Chthonic Project Nov 19 '13 at 3:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.