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

    Default Understanding this recursion

    Hi, could you please help me understand the following case.
    I wrote a method to convert numbers to roman numerals, I insert all the characters into a string variable but the output of the string comes out as the first one character. This is a recursive method and I know all the letters been inserted correctly as when I type system.out.print(roman), it prints all the characters but in a reverse order.

    I curious to know why is that happen and could you please tell me how to reverse it.

    Here is a piece of my code:
    ---------------------------------------------------------------------
    public static String nums2Rom (int a){

    String roman = "";

    if (a >=1000){
    roman+='M';
    int2Rom (a-1000);
    return roman+'M';}

    if (a >=500 && a<1000){
    roman+='D';
    int2Rom (a-500);}
    //...
    //and ends like this:

    if (a == 0) {
    return roman;
    }
    else{
    return roman;

    //Thank you.

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

    Default

    What is int2Rom? You never show us that method. And where does its return value get stored...?

    And why do you return the same thing regardless of the value of "a" at the end of the method?

  3. #3
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default here's an update

    The method gets a number lets say: 4321 and converts it to roman numerals like that: MMMMDDDCCI. ex. when a number is bigger than a 1000 it will print M and subtract 1000 from the number and so on.. until it's 0.
    It works when I System.out the string but at the reversed order and when I return string it will return only M.

    -----------------------------------------------------------------------
    public static String nums2Rom (int a){

    String roman = "";

    if (a >=1000){
    roman+='M';
    nums2Rom (a-1000);
    return roman+'M';}

    if (a >=500 && a<1000){
    roman+='D';
    nums2Rom (a-500);}

    //... the definition continues here.
    //[COLOR="rgb(65, 105, 225)"]and ends like this:[/COLOR]

    if (a == 0) {
    return "";
    }
    else{
    return roman;

  4. #4
    imorio is offline Senior Member
    Join Date
    Aug 2010
    Posts
    127
    Rep Power
    0

    Default

    Say a number is larger than 1000, say 13450.

    String roman = ""; roman==""

    if (a >=1000){ true
    roman+='M'; roman=="M"
    nums2Rom (a-1000);
    return roman+'M';} roman=="M"

    the return value is MM. That is the case for any value for a above 0. Hint: try merging the last 2 lines.

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

    Default

    Quote Originally Posted by Yakg View Post
    The method gets a number lets say: 4321 and converts it to roman numerals like that: MMMMDDDCCI. ex. when a number is bigger than a 1000 it will print M and subtract 1000 from the number and so on.. until it's 0.
    It works when I System.out the string but at the reversed order and when I return string it will return only M.
    I understand that... but the code you pasted the second time was not the same as the first, for reasons I cannot figure out.

    Regardless, imorio is right. It's returning the string value "" (returned when a == 0) with an added "M" on the end, hence "M".
    Last edited by Zack; 01-05-2011 at 08:12 PM.

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

    Default You right it is not the same I've changed names and

    other things, I didn't understand Omirio's solution but appreciate it anyway.

  7. #7
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    59
    Rep Power
    0

    Default I have an alternative method I've written..

    I know it's long but I really appreciate if you could help me figure out why instead of getting the length of each digit I'm getting the length of the first digit. For ex. if I put 4321 it comes out as: MMMMCCCCXXXXIIII.
    -------------------------------------------------------------------------

    public static String int2Rom (int a){
    String numstr = (String) ""+a;
    int digs = numstr.length()-1; // set the length of the number.
    int num=0;
    switch (digs) {
    case 3:num = a/1000; break;
    case 2: num = a/100; break;
    case 1: num = a/10; break;
    case 0: num = a/1; break;
    }
    if (a == 0) //base case.
    return "";
    else {
    return converter (digs, num, a);
    }
    }
    // ------question 4. ----- Helper method ----.
    public static String converter (int digs, int num, int a){
    String array [] [] = {{"V","I"},{"L","X"},{"D","C"},{"5M","M"}};
    String roman = "";
    int bs;
    if (num ==0) //base case
    return num2Rom (a/10);
    else{
    if (num >=5){
    bs = 0;
    roman+= array [digs] [bs];
    num-=5;
    converter (digs, num, a);
    }
    if (num <5){
    bs = 1;
    System.out.print (array [digs] [bs]);
    num--;
    converter (digs, num, a);
    }}
    return roman;
    }
    }

Similar Threads

  1. Vector understanding
    By counterfox in forum New To Java
    Replies: 6
    Last Post: 05-04-2010, 10:59 AM
  2. need help in understanding collection
    By ShinTec in forum Advanced Java
    Replies: 2
    Last Post: 04-24-2010, 02:49 AM
  3. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 06:26 PM
  4. Help on understanding a program
    By newbie225 in forum New To Java
    Replies: 1
    Last Post: 11-10-2009, 12:53 AM
  5. Understanding Vectors
    By cbrown08 in forum New To Java
    Replies: 7
    Last Post: 11-05-2007, 06:56 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
  •