1. Member
Join Date
Dec 2010
Posts
59
Rep Power
0

## Understanding this recursion

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. 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. Member
Join Date
Dec 2010
Posts
59
Rep Power
0

## 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. Senior Member
Join Date
Aug 2010
Posts
127
Rep Power
0
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. Originally Posted by Yakg
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 09:12 PM.

6. Member
Join Date
Dec 2010
Posts
59
Rep Power
0

## 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. Member
Join Date
Dec 2010
Posts
59
Rep Power
0

## 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;
}
}

#### Posting Permissions

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