# help with recursion methods

• 11-05-2010, 06:28 PM
hoosierfan24
help with recursion methods
Hello for my java programing class i have to do this lab that counts how many vowels are in the word and it can not use loops or *
• 11-05-2010, 07:05 PM
Zack
Well... I'd probably have a method that finds the index of the next vowel. If it's greater than or equal to zero, then increment a counter which is returned from each method. Otherwise, return the existing count and stop recursing. Something like this:
Code:

```String x, "sequoias" int count = find_vowels(x, 0, 0) public int find_vowels(str, index, count)     // we need our original string, a starting index to find the NEXT vowel, and a maintained count     store nextA, nextE, nextI, nextO, and nextU based on their indices from indexOf     find lowest value of nextA, nextE, nextI, nextO, and nextU     if (all indices are < 0) return count     return find_vowels(str,index+1,count+1) // increment search index and total count```

PS: Just put this together with actual code and the concept works.
• 11-06-2010, 05:01 PM
hoosierfan24
ok that kind of makes sense but the framework required to do this is

Code:

``` System.out.println( countVowels( "coconut" ) ); public static int countVowels( String s ) { }```
• 11-06-2010, 05:08 PM
hoosierfan24
i know how to calculate the vowels in a for loop but i cant understand how to do it with recursion here is how i would do it in a loop

Code:

```import java.lang.String; import java.io.*; import java.util.*; public class CountVowels{ public static void main(String args[])throws IOException{   BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));   System.out.println("Enter the String:");   String text = bf.readLine();   int count = 0;     for (int i = 0; i < text.length(); i++) {         char c = text.charAt(i);         if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {         count++;         }     }     System.out.println("There are" + " " + count + " " + "vowels"); } }```
• 11-06-2010, 05:39 PM
hoosierfan24
i figured it out and used this as my code

Code:

```   public static int countVowels( String s )   {     if ( s.length() == 0 )       return 0;     if ( "aeiouAEIOU".indexOf( s.substring( 0, 1 ) ) < 0 )       return countVowels( s.substring( 1 ) );     else       return 1 + countVowels( s.substring( 1 ) );   }```
• 11-06-2010, 06:19 PM
JosAH
Quote:

Originally Posted by hoosierfan24
i figured it out and used this as my code

Code:

```   public static int countVowels( String s )   {     if ( s.length() == 0 )       return 0;     if ( "aeiouAEIOU".indexOf( s.substring( 0, 1 ) ) < 0 )       return countVowels( s.substring( 1 ) );     else       return 1 + countVowels( s.substring( 1 ) );   }```

Well done but change that first substring(0, 1) to charAt(0) (I know, it's a minor nitpick but I am like that ;-)

kind regards,

Jos
• 11-16-2012, 05:35 AM
mophiejoe
Re: help with recursion methods
why did you put ( on line 6)
< 0??

and why do you have to have ( on line 7)
s.substring( 1 )

I am just trying to learn, and dont understand.

thank you!

Quote:

Originally Posted by hoosierfan24
i figured it out and used this as my code

Code:

```   public static int countVowels( String s )   {     if ( s.length() == 0 )       return 0;     if ( "aeiouAEIOU".indexOf( s.substring( 0, 1 ) ) < 0 )       return countVowels( s.substring( 1 ) );     else       return 1 + countVowels( s.substring( 1 ) );   }```

• 11-16-2012, 05:38 AM
mophiejoe
Re: help with recursion methods
why do you have on line 6

< 0?

and on line 7

s.substring(1)

sorry just trying to learn

thanks

Quote:

Originally Posted by JosAH
Well done but change that first substring(0, 1) to charAt(0) (I know, it's a minor nitpick but I am like that ;-)

kind regards,

Jos