Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By hoosierfan24
  • 1 Post By JosAH

Thread: help with recursion methods

  1. #1
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default 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 *
    It has to use recursive methods and i have no idea where to start please help.

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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:
    Java 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.

  3. #3
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    ok that kind of makes sense but the framework required to do this is

    Java Code:
    System.out.println( countVowels( "coconut" ) ); 
    
    public static int countVowels( String s )
    {
    
    
    
    }

  4. #4
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    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

    Java 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");
    }
    }

  5. #5
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    i figured it out and used this as my code

    Java 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 ) );
      }
    Jingoism likes this.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

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

    Java 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
    Jingoism likes this.

  7. #7
    mophiejoe is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default 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 View Post
    i figured it out and used this as my code

    Java 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 ) );
      }

  8. #8
    mophiejoe is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default 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 View Post
    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

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 06:26 PM
  3. Recursion with static and non static methods
    By sh4dyPT in forum New To Java
    Replies: 14
    Last Post: 03-27-2009, 06:56 AM
  4. help with recursion
    By Nari in forum New To Java
    Replies: 15
    Last Post: 04-24-2008, 09:13 AM
  5. Help With Recursion
    By andrew777 in forum New To Java
    Replies: 1
    Last Post: 03-29-2008, 12:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •