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 8 of 8

Thread: Decimal to binary conversion

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Decimal to binary conversion

    I am new to the java world and I am currently taking a Programming I class. My homework assignment for the week is to write a java program using methods to allow a user to enter in a decimal number(base 10) and have the program convert it to a binary number. Just remember I am new to this so I don't know everything. I need some input on how finish finish the program I have wrote. It compiles, runs, but only allows the user to enter in a decimal number. It won't convert it. Can someone please help me with what I am doing wrong?
    Here is what I have so far:

    import java.util.Scanner;
    public class Decimal{
    public static void main (String [] args){
    Scanner input = new Scanner(System.in);
    System.out.println ("Enter a decimal number: ");
    int decimal = input.nextInt ();
    System.out.println ("Binary number is: " + decimalToBinary(decimal));
    }
    public static String decimalToBinary(int decimal) {
    int i = 42;
    String decimalToBinary = Integer.toBinaryString(i);
    while (decimal !=0){
    int binaryValue = decimal % 16;
    decimal = decimal/16;
    }
    return decimalToBinary;
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Decimal to binary conversion

    It won't convert it.
    What output does the program generate? Please execute the program and copy and paste here the console showing the output.
    And then explain what the problem is with the output.

    What is your algorithm for converting a decimal String to a binary String.
    For example: "6" to "110"

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Decimal to binary conversion

    Decimal.docAttached is what the output is. It is not calculating right I don't think. When I type a number in for the input it keeps giving me a 101010 for the output. You will see in the attached file. I have searched google for the algorithm for converting a decimal to a binary and I am having no luck. I thought somebody on here who knew more would be able to help me out.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Decimal to binary conversion

    What is the purpose of this line in your program:
    int i = 42;

    Can you explain what each line in your decimalToBinary method is there for? Why did you code this:
    int binaryValue = decimal % 16;

    You need to develop an algorithm for the conversion,
    If you can't find one online.
    Do you understand what a binary number is?
    For example what is the binary representation for 5?
    When you get that, figure out how you can compute it.
    Hint: use modulo 2 and divide by 2. Work from right to left in building the output.
    Last edited by Norm; May 21st, 2011 at 11:51 AM.

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Decimal to binary conversion

    Quote Originally Posted by Norm View Post
    What is the purpose of this line in your program:
    int i = 42;

    Can you explain what each line in your decimalToBinary method is there for? Why did you code this:
    int binaryValue = decimal % 16;

    You need to develop an algorithm for the conversion,
    If you can't find one online.
    Do you understand what a binary number is?
    For example what is the binary representation for 5?
    When you get that, figure out how you can compute it.
    Hint: use modulo 2 and divide by 2. Work from right to left in building the output.
    Honestly I am too new at this to completely understand what exactly I am supposed to do. To be completely honest our teacher said the code we are supposed to come up with is only 3 lines different than the example she gave us out of the book. I do understand what a binary number is, I'd have to look it up because I don't remember them well enough to have it off the top of my head. I am not 100 % sure what the int i=42 is for nor the int binaryValue = decimal % 16. That is why I am looking for help. I have tried emailing my teacher and got no response. I have looked for hours online for an algorithm and help and I saw something about an intParse for converting decimal to binary but I am just so confused now that I don't know what to use. I guess I honestly don't know what to do at all. I apologize!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Decimal to binary conversion

    Even before designing an algorithm, you need to understand the problem. What is the input to the program and what is the output.
    For example if the input to the program is 9 the output should be: "1001". Notice the 9 is an int value and the "1001" is a String

    Before you write any code, you need to have an algorithm: the steps needed to solve the problem. First work on getting the algorithm, then figure out how to write a program to implement it.
    I gave a hint at the end of my post:
    For example what is the binary representation for 5?
    When you get that, figure out how you can compute it.
    Hint: use modulo 2 and divide by 2. Work from right to left in building the output.

    Write down the binary value for 13. Then get the modulo 2 value and write it down. Then divide by 2.
    Then get modulo 2 value of the new number. Then divide the new number by 2.
    Continue until then new number is zero.

    If you look at the modulo results they should be the binary value of the number.
    Last edited by Norm; May 21st, 2011 at 12:59 PM.

  7. #7
    Junior Member
    Join Date
    May 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Decimal to binary conversion

    Quote Originally Posted by Norm View Post
    Even before designing an algorithm, you need to understand the problem. What is the input to the program and what is the output.
    For example if the input to the program is 9 the output should be: "1001". Notice the 9 is an int value and the "1001" is a String

    Before you write any code, you need to have an algorithm: the steps needed to solve the problem. First work on getting the algorithm, then figure out how to write a program to implement it.
    I gave a hint at the end of my post:
    For example what is the binary representation for 5?
    When you get that, figure out how you can compute it.
    Hint: use modulo 2 and divide by 2. Work from right to left in building the output.

    Write down the binary value for 13. Then get the modulo 2 value and write it down. Then divide by 2.
    Then get modulo 2 value of the new number. Then divide the new number by 2.
    Continue until then new number is zero.

    If you look at the modulo results they should be the binary value of the number.
    Ok, after taking the rest of the day to figure out how to convert a decimal number into a binary number I will have to say that I can successfully do it. I am still stumped on coming up with an algorithm. I guess I just don't have enough experience with coming up with more complex algorithms. All of our other homework has been a lot more simple compared to this. We have never once talked about converting decimal to binary in class. It is something I had to figure out on my own. We talked about methods and strings for about 30-40 minutes so I am not fully aware how to use them. A lot of the terms you are using I have never even heard of (i.e. modulo 2). I have only been in class a few weeks and I have no other programming experience. So when I mean I am new, I am new to this stuff. I hate being the type of person looking for a handout but I am just not fully understanding this weeks assignment. I feel like my teacher didn't explain this section enough. My question to you is can you give me the correct code and comment in it what you are doing and why? It would also be nice to know what I am doing wrong as well. I truely need help and want to learn as this is my future and quite possibly my career so I really need to learn. I know I am asking for a lot but I have been trying to get ahold of my teacher for 3 days now and have got no response. I have also contacted 2 of my buddies (one has a masters in i.t. and works for vera bradley but has no programming experience except for sql and the other is a computer engineer for general motors and has only worked with visualbasic) he is the one who helped me with understanding the decimal to binary conversion. I truely appreciate all the help you have given me already! Thanks

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,552
    Thanks
    45
    Thanked 1,953 Times in 1,927 Posts

    Default Re: Decimal to binary conversion

    I can successfully do it
    Now write down the steps that you take to convert a decimal number to a binary one.
    Make them very simple steps. Only do one thing at a time.
    Given that, we can help you convert the steps to program statements.

Similar Threads

  1. Converting Hex to Decimal
    By r2ro_serolf in forum Java Theory & Questions
    Replies: 10
    Last Post: September 4th, 2011, 04:29 PM
  2. [SOLVED] decimal to binary problem
    By mick.bhattarai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 26th, 2010, 06:48 PM
  3. decimal and hexdecimal
    By ran830421 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 21st, 2009, 01:58 AM
  4. decimal to hex
    By rsala004 in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 3rd, 2009, 01:16 AM
  5. binary conversion..
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: September 16th, 2009, 10:47 AM