Results 1 to 10 of 10
Thread: binary _ array
- 12-02-2010, 06:44 PM #1
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
- 12-02-2010, 07:12 PM #2
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
Here's a good starting point: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
:)
Chris:cool: It's all here: http://download.oracle.com/javase/6/docs/api/
- 12-02-2010, 07:23 PM #3
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
- 12-02-2010, 07:27 PM #4
Well, this depends. How are you getting the binary number? Are you going to be passed 1's and 0's via a string. Are you going to be passed an integer? Can the integer be signed or unsigned?
I can't give you an unambiguous response if you give me an ambiguous question.
- 12-02-2010, 07:37 PM #5
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
ok
this my code i want the program to take the binary number and but every element in arrayPHP Code:try{ System.out.println("Enter your multiplier"); multiplier=sc.nextInt(); StringBuilder sb = new StringBuilder(); bin = Integer.toBinaryString(multiplier); for (int i = bin.length(); i < limit; i++) { sb.append("0"); } sb.append(bin); System.out.println(sb.toString()); } catch(Exception e){ System.out.println("Enter number *_^"); }
Ex:
0101
i want to take the first element(1) .............
to take it we should put it in array to make address for it as [0]=1, right??
- 12-02-2010, 07:49 PM #6
That did not help much. However, I see that you are using the toBinaryString method from Integer. Since you have a string of all the binary digits, you simply have to split them up and put them in the array.
This also brings about the question of whether you want an array of integers, (0 or 1) or characters ('0' or '1').
Either way, you can get each individual character in the string using the charAt(int index) method. You can create a for loop which goes through and gets each character in the string, then you can handle each character how you wish.
- 12-02-2010, 07:50 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Use a for loop. The StringBuilder method charAt() will give you the character at each position and all you need to do is look at it and decide whether to put a 0 or a 1 into the array.
Note that int arrays are initialised with zeros in them so you might be able to avoid padding the bin string with zero digits.
- 12-02-2010, 07:56 PM #8
Member
- Join Date
- Aug 2010
- Location
- Egypt
- Posts
- 34
- Rep Power
- 0
sorry,but i cant understand
please,can any one put acode to right understand
thanks
- 12-02-2010, 08:02 PM #9
That is the basics of what you want to do. You have to find out the specifics for yourself, as it is your problem.Java Code:String s = Integer.toBinaryString(x); for(int i = 0; i < s.length(); i++) { char digit = s.charAt(i); // Do what you want with digit here }
- 12-03-2010, 05:28 AM #10
char[] ch = bin.toCharArray();
you can use it as you want .....
The best possible way is as described above by using a for loop ....
if you want the reverse of the string and then to array .... then use a StringBuilder and its reverse function and then to char Array...
Lets be more specific so that guys in here can help u out :)
warm regards
Vinod MLast edited by Vinod Mukundan; 12-03-2010 at 05:32 AM.
_______________________________________________
give me beans .........
Similar Threads
-
Binary
By THEAniKan in forum EntertainmentReplies: 1Last Post: 01-06-2012, 10:14 AM -
"Array Map" with Binary Search...
By kreyszig in forum Advanced JavaReplies: 5Last Post: 10-14-2010, 02:23 PM -
Decimal to Binary "Using Array"
By pinkdreammsss in forum Java AppletsReplies: 10Last Post: 04-23-2010, 06:21 PM -
Having trouble insert/sorting array values w/ binary searching.
By bh-chobo in forum New To JavaReplies: 2Last Post: 10-07-2009, 06:24 PM -
get binary value
By rushenas in forum New To JavaReplies: 2Last Post: 06-14-2008, 01:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks