# Decimal to hex problem

• March 4th, 2014, 09:38 AM
kennylty
Decimal to hex problem
Why do i not get a value 0 when my input is 0.

Code java:

```import java.util.Scanner;   public class Dec2Hex { public static void main(String[] args){ int dec; String hexStr = ""; int radix = 16; char[] hexChar = {'0', '1', '2', '3', '4','5','6','7','8','9', 'A', 'B', 'C', 'D', 'E', 'F'};   //Read input Scanner in = new Scanner(System.in); System.out.print("Enter an interger: "); dec = in.nextInt();   while(dec>0) { int hexDigit = dec % radix; hexStr = hexChar[hexDigit] + hexStr; dec = dec / radix; }   System.out.println("The hexadecimal equivalent is: "+hexStr); } }```
• March 4th, 2014, 09:41 AM
Norm
Re: Decimal to hex problem
Can you post the contents of the console from when the program is executed showing what you are asking about?

[code=java]
[/code]
to get highlighting and preserve formatting.
• March 4th, 2014, 09:45 AM
kennylty
Re: Decimal to hex problem
Thanks for the reply, this is my first post
• March 4th, 2014, 09:52 AM
andbin
Re: Decimal to hex problem
Quote:

Originally Posted by kennylty
Why do i not get a value 0 when my input is 0.
Code java:

`while(dec>0) {`

See the condition ;)
And it's not sufficient to change the condition (otherwise you'll go in "loop").
• March 4th, 2014, 10:03 AM
kennylty
Re: Decimal to hex problem
Quote:

Originally Posted by andbin
See the condition ;)
And it's not sufficient to change the condition (otherwise you'll go in "loop").

but if i change it to
Code java:

`while (dec>=0)`
it does not return the println at all.
• March 4th, 2014, 10:06 AM
andbin
Re: Decimal to hex problem
Quote:

Originally Posted by kennylty
but if i change it to
Code java:

`while (dec>=0)`
it does not return the println at all.

I told "And it's not sufficient to change the condition (otherwise you'll go in "loop")."

You don't need a while .... you need a do-while (with same condition as before).
• March 4th, 2014, 10:12 AM
kennylty
Re: Decimal to hex problem
Quote:

Originally Posted by andbin
I told "And it's not sufficient to change the condition (otherwise you'll go in "loop")."

You don't need a while .... you need a do-while (with same condition as before).

Wow! i got it already. thanks so much. Need to read up the different uses of loops again