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 🙂
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 🙂