Results 1 to 6 of 6
- 03-06-2012, 10:11 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
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.
- 03-06-2012, 02:15 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
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);
- 03-06-2012, 02:17 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
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.
- 03-06-2012, 02:18 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
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.
- 03-06-2012, 04:37 PM #5
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
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 03-07-2012, 05:10 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
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
-
Concatenation with strings
By link6790 in forum New To JavaReplies: 16Last Post: 05-05-2011, 08:33 PM -
Writing a java applet that converts numbers to words
By derekmski in forum Java AppletsReplies: 1Last Post: 04-16-2011, 04:33 AM -
Binary Strings
By Zack in forum New To JavaReplies: 0Last Post: 06-25-2010, 08:15 AM -
Sum binary numbers
By lobodelbosque in forum New To JavaReplies: 3Last Post: 12-14-2009, 05:44 AM -
Converts a binary number to a decimal
By cachi in forum New To JavaReplies: 1Last Post: 08-01-2007, 10:57 AM
Bookmarks