Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Java Program that converts numbers to binary using strings concatenation some how.

  1. #1
    lemony7 is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Java Program that converts numbers to binary using strings concatenation some how.

    Java Code:
     * 
     */
    import java.util.Scanner;
    
    public class Bits {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		int n = in.nextInt();
    		String binary;
    		while (n > 0) {
    			binary = "" + n % 2;
    			n = n / 2;
    			System.out.print(binary);
    		}
    	}
    }
    /**

    This is what I have so far. I understand there is an easier way of converting numbers into strings using a stringbuilder but my professor wants me to use string concatenation to get a binary output.

    The instruction says to use a string to accumulate the results which I believe I did and to use string concatenation for each digit instead of integer addition. I tried using substring to reverse the order or even charAt so I can at least reverse the order and concatenate it myself but my java compiler won't let me do that and says it is out of range or something when i tried substring and charAt.

    So I have no idea how to reverse my output so It can be properly in binary using string concatenation only. Please help.
    Last edited by lemony7; 03-06-2012 at 07:51 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Java Program that converts numbers to binary using strings concatenation some how

    Actually the way you concatenate is not correct.

    Java Code:
            String binary = "";
            int val = 0;
            while (n > 0) {
            	val = (n % 2);
            	n = n / 2;
    
                binary = (val) + binary;
            }
            System.out.print(binary);

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Java Program that converts numbers to binary using strings concatenation some how

    Apart from that you can do this with toBinaryString(). Read wrapper Integer to find out more.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Java Program that converts numbers to binary using strings concatenation some how

    Please find the link below.

    Java 2 Platform SE v1.3.1: Class Integer

    And also if you think it is worth to have a look at the exception you end up with in charAt() something post it here. We can discuss.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Java Program that converts numbers to binary using strings concatenation some how

    Um, not Java 1.3!

    Java Platform SE 6
    Java Platform SE 7

    db
    Eranga likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Java Program that converts numbers to binary using strings concatenation some how

    Oh yes db. I pick the wrong library from my collection. Thanks for the update.

Similar Threads

  1. Concatenation with strings
    By link6790 in forum New To Java
    Replies: 16
    Last Post: 05-05-2011, 08:33 PM
  2. Writing a java applet that converts numbers to words
    By derekmski in forum Java Applets
    Replies: 1
    Last Post: 04-16-2011, 04:33 AM
  3. Binary Strings
    By Zack in forum New To Java
    Replies: 0
    Last Post: 06-25-2010, 08:15 AM
  4. Sum binary numbers
    By lobodelbosque in forum New To Java
    Replies: 3
    Last Post: 12-14-2009, 05:44 AM
  5. Converts a binary number to a decimal
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 10:57 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
  •