Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Decimal to hex problem

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Decimal to hex problem

    Why do i not get a value 0 when my input is 0.

    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);
    	}
    }
    Last edited by kennylty; March 4th, 2014 at 11:46 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Decimal to hex problem

    Thanks for the reply, this is my first post

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Decimal to hex problem

    Quote Originally Posted by kennylty View Post
    Why do i not get a value 0 when my input is 0.
    while(dec>0) {
    See the condition
    And it's not sufficient to change the condition (otherwise you'll go in "loop").
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Decimal to hex problem

    Quote Originally Posted by andbin View Post
    See the condition
    And it's not sufficient to change the condition (otherwise you'll go in "loop").
    but if i change it to
    while (dec>=0)
    it does not return the println at all.

  6. #6
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Decimal to hex problem

    Quote Originally Posted by kennylty View Post
    but if i change it to
    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).
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Decimal to hex problem

    Quote Originally Posted by andbin View Post
    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

Similar Threads

  1. Replies: 1
    Last Post: May 13th, 2013, 05:09 PM
  2. Converting Hex to Decimal
    By r2ro_serolf in forum Java Theory & Questions
    Replies: 10
    Last Post: September 4th, 2011, 04:29 PM
  3. Hex to Integer Problem
    By TempSpectre in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2010, 06:01 AM
  4. print hex decimal value
    By ran830421 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 25th, 2009, 07:23 PM
  5. decimal to hex
    By rsala004 in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 3rd, 2009, 02:16 AM