Results 1 to 9 of 9
  1. #1
    aspire007 is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Exclamation Convert Decimal To Binary

    i have the code:

    import java.io.*;

    public class Convert{
    public static void main(String args[]) throws IOException {

    int a[] = {0, 1};

    int number;
    int r;

    BufferedReader Input = new BufferedReader( new InputStreamReader(System.in));

    System.out.println();
    System.out.print("Enter Decimal Number: ");
    number = Integer.parseInt( Input.readLine());
    System.out.println();

    do{

    r=number%2;

    if(r > 0){
    System.out.println(a[1]); //prints the binary 1
    }

    else{
    System.out.println(a[0]); //prints the binary 0
    }

    number=number / 2;
    }while(number>0);

    System.out.println();
    System.out.print("Read the answer from bottom to top!");
    }
    }

    and the output is:
    --------------------------------
    Enter Decimal Number: 10

    0
    1
    0
    1

    Please read from bottom to top!
    --------------------------------

    Please help me on how to make the output like below using the following codes above. Thanks for those who help me...!
    ------------------------
    Enter Decimal Number: 10

    Binary : 1010
    ------------------------

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Think about this: you have four strings (0, 1, 0, 1). If you were to store these in a variable, you could have: "0101". Then you could reverse it to get "1010".

    Your method may not be the best but it appears to work for you--so what I'm suggesting is that instead of using println() for each number you print, create an empty string that you prepend each time you have another number. So it would be "0", "10", "010", "1010".

    Much luck! :)

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Do you have to print the binary digits as you compute them or can you save them and print them all after they are all available?
    If you can save them, use String concatenation to save them them into a String and then print that String when done.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    if you are only interested in the string-result, and not each single bit, you can also replace your do-while-loop with the following code

    Java Code:
    		System.out.println("Binary: "
    				+ Long.toBinaryString(Long.parseLong(String.valueOf(number))));

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Calling someone else's methods doesn't demonstrate that the student knows how to solve the problem.

  6. #6
    aspire007 is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default

    I think it is the shortcut method where in it automatically converts the decimal to binary to its correct output position. Our instructor said that we do not use the shortcut method.."

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Unfortunately the method you are using can only work this way (in the reverse direction). So you have two choices:

    1) Use String prepending (concatenation to the start of the string), then print the string.
    2) Use a different method (I can show you a different one).

  8. #8
    aspire007 is offline Member
    Join Date
    Jul 2010
    Posts
    4
    Rep Power
    0

    Default

    Zack can you show me the different one method you said? Can you help me cause its an assignment, i am only newbie and i want to learn...i've tried searching about the string prepending or the concatenation you said but i dont have found sample..i'm running out of time for the submission to our instructor..hope you can help me..."

  9. #9
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Have you done string concatenation? Like...
    Java Code:
    String x = "a" + "b";
    Because if you have, then you can use this for the assignment.

    Alternately, here is another type of code you can use:
    How to Convert from Decimal to Binary - wikiHow
    (The second method is what you used; the first method is what I'm suggesting.)

Similar Threads

  1. Binary to Decimal Converter
    By c_walker in forum New To Java
    Replies: 15
    Last Post: 11-24-2009, 02:38 PM
  2. Replies: 4
    Last Post: 10-26-2009, 10:17 PM
  3. Convert binary into decimal
    By WarmRegards in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 02:32 PM
  4. Eclipse- Decimal to binary
    By queen_vee in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 02:17 PM
  5. Convert decimal to binary..pls help..newbie here
    By mephisto772 in forum New To Java
    Replies: 5
    Last Post: 02-12-2009, 08:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •