Results 1 to 7 of 7
Thread: Understanding this recursion
 01052011, 07:49 PM #1Member
 Join Date
 Dec 2010
 Posts
 63
 Rep Power
 0
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 (a1000);
return roman+'M';}
if (a >=500 && a<1000){
roman+='D';
int2Rom (a500);}
//...
//and ends like this:
if (a == 0) {
return roman;
}
else{
return roman;
//Thank you.
 01052011, 08:06 PM #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?
 01052011, 08:21 PM #3Member
 Join Date
 Dec 2010
 Posts
 63
 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 (a1000);
return roman+'M';}
if (a >=500 && a<1000){
roman+='D';
nums2Rom (a500);}
//... the definition continues here.
//[COLOR="rgb(65, 105, 225)"]and ends like this:[/COLOR]
if (a == 0) {
return "";
}
else{
return roman;
 01052011, 08:48 PM #4Senior 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 (a1000);
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.
 01052011, 09:09 PM #5
Last edited by Zack; 01052011 at 09:12 PM.
 01052011, 09:22 PM #6Member
 Join Date
 Dec 2010
 Posts
 63
 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.
 01052011, 10:39 PM #7Member
 Join Date
 Dec 2010
 Posts
 63
 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;
}
}
Similar Threads

Vector understanding
By counterfox in forum New To JavaReplies: 6Last Post: 05042010, 10:59 AM 
need help in understanding collection
By ShinTec in forum Advanced JavaReplies: 2Last Post: 04242010, 02:49 AM 
recursion and tailrecursion differences
By OptimusPrime in forum New To JavaReplies: 2Last Post: 12282009, 07:26 PM 
Help on understanding a program
By newbie225 in forum New To JavaReplies: 1Last Post: 11102009, 01:53 AM 
Understanding Vectors
By cbrown08 in forum New To JavaReplies: 7Last Post: 11052007, 07:56 PM
Bookmarks