# Understanding this recursion

• 01-05-2011, 07:49 PM
Yakg
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.
• 01-05-2011, 08:06 PM
Zack
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?
• 01-05-2011, 08:21 PM
Yakg
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;
• 01-05-2011, 08:48 PM
imorio
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.
• 01-05-2011, 09:09 PM
Zack
Quote:

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".
• 01-05-2011, 09:22 PM
Yakg
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.
• 01-05-2011, 10:39 PM
Yakg
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;
}
}