Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Recursive Method to count vowel

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Recursive Method to count vowel

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter a string: ");
    String word = input.nextLine();
    int i = 0, count = 0;
    checkVowel(word, i, word.length()-1, count);
    }

    public static void checkVowel(String word, int start, int last, int count) {
    if (start == last)
    {
    System.out.println("Total number of vowel is " + count);
    }
    else
    {
    if (word.charAt(start) == 'a' || word.charAt(start) == 'e' || word.charAt(start) == 'i' ||
    word.charAt(start) == 'o' || word.charAt(start) == 'u') {
    count++;
    }
    checkVowel(word, start++, last, count);
    }
    }


    this is my code, but somehow i gt "Exception in thread "main" java.lang.StackOverflowError" , can anyone help find what's the problem??


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Recursive Method to count vowel

    When posting code, please use highlight tags.

    What happened when you stepped through this with a debugger, or at least added print statements?

    Hint: What is the value of start each time the function is called?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursive Method to count vowel

    public static void checkVowel(String word, int i, int last, int count) {
            if (i == last)
            {
                System.out.println("Total number of vowel is " + count);
            }
            else
            {  
               [B] i++;[/B]
                if (word.charAt(i) == 'a' || word.charAt(i) == 'e' || word.charAt(i) == 'i' ||
                    word.charAt(i) == 'o' || word.charAt(i) == 'u') {
                    count++;    
                }
                checkVowel(word, i, last, count);
            }           
        }

    Thx for the hints..... sorry about the highlight, will keep it in mind.......

  4. #4
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursive Method to count vowel

    The problem lies in the recursive function, which must return something to get the previous level where it was invoked. Pleaes try the following:
    public class RecursiveFunctionTest {

    private static final String VOWELS = "aeiouAEIOU";

    public static int checkVowels(String word, int start, int end, int count){
    if(start >= end){
    return count;
    }
    else{
    if(VOWELS.indexOf(word.charAt(start)) != -1)
    count++;
    }

    return checkVowels(word, ++start, end, count);
    }

    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a String: ");
    String word = input.nextLine();
    int count = 0;
    count = checkVowels(word, 0, word.length()-1, count);
    System.out.println("The total number of vowels is " + count);
    }
    }

  5. #5
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Recursive Method to count vowel

    No, the recursive function mustn't return a value in order to terminate. For example, this function is OK:
        public static void f(int x, int term) {
            if(x >= term) {}
            else {
                System.out.println(x + " " + term);
                f(x + 1, term);
            }
        }

    StackOverflowException means, that the function can't terminate. Let's have a look at the termination condition:
            if (i == last)
            {
                System.out.println("Total number of vowel is " + count);
            }
    The function terminates only when i == last. I suspect, that at some point you may erroneously call the function with i > last. In this case function will never terminate, because i increases from call to call. Try "i >= last" termination condition instead of "i == last".

  6. #6
    Junior Member
    Join Date
    Jun 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Recursive Method to count vowel

    Return a value or not does not matter for a recursive function indeed. The core part is to terminate it. So I agree with you that something is wrong with the termination condition. So in my example, I used ">=" instead.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Recursive Method to count vowel

    Guys, please stop trying to do his homework for him- Gary, the advice you offered is incorrect.

    The problem lies in the parameters being passed into the recursive call. It's not helpful to simply fix the code for him. I have suggested that the OP add some print statements or step through this with a debugger to figure it out on his own, which is what he should be doing.

    OP, did you fix your problem?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. How to create this recursive method.
    By exodus2041 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2013, 10:26 AM
  2. help with recursive method
    By mflb94 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2012, 06:30 PM
  3. Do While loop + switch, Vowel Count
    By mwardjava92 in forum Loops & Control Statements
    Replies: 3
    Last Post: November 9th, 2011, 11:46 AM
  4. [SOLVED] StackOverflowError with recursive method
    By samfin in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 2nd, 2010, 04:05 PM
  5. Method to count objects
    By mjpam in forum Java Theory & Questions
    Replies: 5
    Last Post: July 28th, 2010, 08:49 AM