How to make a Recursive method to change a Decimal to Hexadecimal ? I am trying but i have problems to make this recursion. please help
How to make a Recursive method to change a Decimal to HexadecimalPlease help
Page 1 of 1
3 Replies - 1748 Views - Last Post: 14 March 2009 - 03:18 PM
#1
How to make a Recursive method to change a Decimal to Hexadecimal
Posted 14 March 2009 - 10:59 AM
Replies To: How to make a Recursive method to change a Decimal to Hexadecimal
#2
Re: How to make a Recursive method to change a Decimal to Hexadecimal
Posted 14 March 2009 - 11:09 AM
Post what you have so far:
[rules][/rules]
[rules][/rules]
#3
Re: How to make a Recursive method to change a Decimal to Hexadecimal
Posted 14 March 2009 - 03:08 PM
this is my code, i am having problems, i dont know where is the problem but it is not working. Please somebody help
import java.util.*;
public class Hexconvertion {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
System.out.println("Please enter the number that you will convert to hex");
int decimalNum=key.nextInt();
}
public static void converttohex(int decimalNumber)
{
String calculated = "";
if(decimalNumber > 0)
{
converttohex(decimalNumber/16);
if(decimalNumber < 10)
{
String hexString = "";
int intChar = (decimalNumber / 16);
switch(intChar)
{
case 10: hexString = "A"; break;
case 11: hexString = "B"; break;
case 12: hexString = "C"; break;
case 13: hexString = "D"; break;
case 14: hexString = "E"; break;
case 15: hexString = "F"; break;
}
System.out.print(intChar+""+hexString);
}else
{
System.out.print((decimalNumber % 16));
}
}
}
}
#4
Re: How to make a Recursive method to change a Decimal to Hexadecimal
Posted 14 March 2009 - 03:18 PM
Well you are making the method recursive and not returning any thing, so since the variable isn't a class variable then you have problems, you will have to return type String to build each part of Hex representation String.
An idea: check if number < 16 if it is and have a "helper" method to convert a number 0-15 to a String hex representation, if they are greater than that then do the % to get the correct and make the recursive call in that return
An idea: check if number < 16 if it is and have a "helper" method to convert a number 0-15 to a String hex representation, if they are greater than that then do the % to get the correct and make the recursive call in that return
This post has been edited by BigAnt: 14 March 2009 - 03:32 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|