Results 1 to 6 of 6
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default very frustrating.. recursive

    Hi, in this example I'm trying to return a string unsuccessfully.
    The input number is 3000 and the output should be MMM, I completely don't understand where I go wrong with this recursive method...

    --------------------------------------------------------------------------
    public static String num2rom (int a){
    String m = "M";

    if (a == 0) {
    return "";
    } else {
    if (a >=1000){
    }
    return helper ("M",a-1000);
    }
    }

    public static String helper (String thou, int a){
    String result = "";
    result+=thou;
    return num2rom (a);
    }

    }

    //thanks

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    What happens when you run it? Whats the output? Or error?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Java Code:
    public static String helper (String thou, int a){
        String result = "";
        result+=thou;
        return num2rom (a);
    }
    You create result, append thou to it and then...do nothing with it.
    It vanishes.

  4. #4
    hosscomp is offline Member
    Join Date
    Oct 2010
    Posts
    63
    Rep Power
    0

    Default

    Java Code:
    } else {
    if (a >=1000){
    }
    Useless IF block

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im a little unfamiliar with java, but I have a good understanding of recursion.
    The problem I see is that you only really need one function.

    Have it take an int amount, if it's less than 1000 it can return the result.

    If you want me to print actual working code I will, just let me know.

    But do this for me, try making one function, which takes a string and a number.

    this should be the template of recursion
    Java Code:
    recursive code(args)
    if it meets a termination condition, end and return the answer.
    else make some change and reapply the function

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Also, if you are somewhat new with the idea of recursion, I find the book, HTDP does a very good job of teaching recursion. I'd say 75% of the exercises in the book are recursion based.

    It's free online at htdp.org, very easy to understand, and I think it's very well written. It uses Dr.racket programming language which is quite different than java and c/c++ but it's nice to get a good understanding if you are a beginner.
    Last edited by sunde887; 01-06-2011 at 10:30 PM.

Similar Threads

  1. get index from an array (very frustrating)
    By Yakg in forum New To Java
    Replies: 4
    Last Post: 12-06-2010, 06:26 PM
  2. Please help setting up Tomcat. Very frustrating.
    By wombatvvv in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 09-12-2010, 05:56 AM
  3. basic help with Recursive
    By syntrax in forum New To Java
    Replies: 3
    Last Post: 12-15-2009, 06:19 AM
  4. Recursive Counting
    By zlwilly in forum New To Java
    Replies: 1
    Last Post: 01-29-2009, 08:42 PM
  5. Help with recursive implementation
    By toby in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 05:57 AM

Posting Permissions

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