• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Recursion in Java

Rockinacoustic

Platinum Member
So I have to alter some code to make a simple base converter in Java, but using recursion. I understand the logic behind base converting but my two mains problems are how to convert each remainder to the string, and then how to call the string after the remainder reaches zero based on the skeleton code I was given.

So far I've gotten this:



import java.util.*;

public class BaseConversion
{

public static void main (String[] args)
{
int base10Num;
int base;
Scanner sc = new Scanner(System.in);

System.out.println ();
System.out.println ("Base Conversion Program");
System.out.print ("Enter an integer: ");
base10Num = sc.nextInt();
sc.nextLine(); // Consume the extraneous newline character

System.out.print ("Enter the base: ");
base = sc.nextInt();
sc.nextLine();

// Call convert and print the answer
System.out.println(convert (base10Num, base));

}

public static String convert (int num, int b)
{
int quotient; // the quotient when num is divided by base b
int remainder; // the remainder when num is divided by base b

quotient = (num / b);
remainder = (num % b);

if (quotient > 0)
{
return ("" + remainder); // Not sure if this is adding the remainder to the String
}
else
return (); // Not sure how to call the string when remainder hits 0.
}

}


Thanks in Advanced 🙂
 
return ("" + remainder); // Not sure if this is adding the remainder to the String
Yes, this will turn remainder into a string. You can also use the Integer class to do this:

Integer.toString(remainder);

Check the java.sun.com site and the API section. Lots of great help there
J2SE version 6 API

return (); // Not sure how to call the string when remainder hits 0.

When the remainder is zero you just get a zero at that place holder. Think about a trivial case, conver 100 to base 10. Your first two operations will give you 0 remainder which is perfectly correct.
 
return (); // Not sure how to call the string when remainder hits 0.

When the remainder is zero you just get a zero at that place holder. Think about a trivial case, conver 100 to base 10. Your first two operations will give you 0 remainder which is perfectly correct.

My bad, I posted without really reading your code, just comments. The comment should probably read 'when quotient hits zero' right?

What you're missing here is building your string. When the quotient is not zero you need to do more work, in your case, actually do the recursion. When the quotient hits zero you're at the end of your recursion and need to start popping back to the top.

It might help for you to draw out the steps and see what the string should look like at each point. That might guide you in how to build your final answer string.
 
A little late but I finally figured it out. I was calling it recursively at first in the 'if' statement without adding the remainder as well to the string. Here's the final code for that part:


if (quotient > 0)
{
return ("" + (convert(num/b, b) + remainder));
}
else
{
return ("" + remainder);
}


Recursion is a wierd method 😕

 
Back
Top