• 03-15-2011, 03:16 PM
stegano
i want to store integer into 200 byte register. This is the code.

Code:

``` int[] register = new int[200]; for(int j = 0; j < 200; j++)         {         register[j] = binary.charAt(j);         register[j] = digit;         System.out.println(register[j]);```
• 03-15-2011, 08:45 PM
ozzyman
What is it you want the code to do?

String.charAt returns a character element, not an integer, so you'll need to convert "binary.charAt(j)" to an Integer before you store it in your "int[] register" array
• 03-15-2011, 09:46 PM
stegano
i've already converted a text into binary.what i want the code to do is to place each of binary bits into the register.for example,let the text be "axcbd" and here is the binary for the text --> a= 1010101 , b 1101010 , c = 1110101, d = 1111010, x = 1111101
I want to make this binary (10101011111101111010111010101111010) as an initialization for the register to run.hopefully,anyone here can help me to fix this problem.tq
• 03-15-2011, 10:03 PM
Junky
Quote:

Originally Posted by stegano
This is the code.

So? What do you want us to do about it? If you ask a specific question then you will get a specific answer.
Code:

`for(int j = 0; j < 200; j++)`
Is your binary String always going to be 200 chars long? If not you will get an IndexOutOfBoundsException
Code:

```register[j] = binary.charAt(j); register[j] = digit;```
Do you realise that the first line is totally pointless? What ever value is inserted into the array on this line is immediately overwritten by the second line.
• 03-15-2011, 10:10 PM
ozzyman
Ah, i see. But the register is an integer array, so once you get the binary representation of each letter you'll need to convert it into a string as you've done in the bracket:
Quote:

I want to make this binary (10101011111101111010111010101111010)
Once you have a long string of all the numbers you can use String.split(String regex) to cut it after each number into an array, and then cast the array of string-numbers into your int register array
• 03-15-2011, 10:12 PM
ozzyman
Quote:

Originally Posted by Junky
Code:

```register[j] = binary.charAt(j); register[j] = digit;```
Do you realise that the first line is totally pointless? What ever value is inserted into the array on this line is immediately overwritten by the second line.

This is also true. If you use a List/ArrayList it would be better because you don't have to define the size before-hand, and then you wouldn't need to convert to integer either because lists can hold objects/any element
• 03-15-2011, 10:29 PM
Junky
and yet still no question
• 03-15-2011, 10:31 PM
ozzyman
but i thought the register was initialised...
Quote:

Code:

`int[] register = new int[200];`

• 03-15-2011, 10:40 PM
stegano
sorry,my mistake.what i need to do to make those register call the each of binary bits and put it in that register?
• 03-15-2011, 10:52 PM
Junky
That is still a statement. Sticking a question mark on the end doesn't make it a question.

You have a binary String which may be shorter than 200 chars long, so you should loop the length of the String and not hard code it to be 200.

For each char in the String convert it to an int and insert into your array.

Done.
• 03-15-2011, 10:55 PM
ozzyman
First convert your binary to a String.
Code:

```String binaryString = "10101011111101111010111010101111010"; String[] registerString = binaryString.split("(?<=\\G.)"); for (i=0;i<registerString.length;i++) {   register[i] = Integer.parseInt(registerString[i]); }```
• 03-15-2011, 11:05 PM
Junky
Using String.split and a regular expression might be overkill in this situation. It can be achieved simply enough with charAt.
• 03-15-2011, 11:10 PM
ozzyman
Anything else would require more logic which is obviously lacking in this case...
• 03-15-2011, 11:18 PM
Junky
No it wouldn't. In fact it requires less.
Code:

```binaryString loop each char in binaryString {     array[postion] = convert char to int; }```
Eliminates an array and the call to String.split. Note I deliberatley have omitted the actual code because it is the OP's job to write it not me.