1. Member
Join Date
Feb 2011
Posts
28
Rep Power
0

i want to store integer into 200 byte register. This is the code.

XML 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]);```

2. 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

3. Member
Join Date
Feb 2011
Posts
28
Rep Power
0
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

4. 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.
Java 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
Java 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.

5. 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:
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

6. Originally Posted by Junky
Java 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

7. and yet still no question

8. but i thought the register was initialised...
Java Code:
`int[] register = new int[200];`

9. Member
Join Date
Feb 2011
Posts
28
Rep Power
0
sorry,my mistake.what i need to do to make those register call the each of binary bits and put it in that register?

10. 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.

11. First convert your binary to a String.
Java Code:
```String binaryString = "10101011111101111010111010101111010";
String[] registerString = binaryString.split("(?<=\\G.)");
for (i=0;i<registerString.length;i++) {
register[i] = Integer.parseInt(registerString[i]);
}```

12. Using String.split and a regular expression might be overkill in this situation. It can be achieved simply enough with charAt.

13. Anything else would require more logic which is obviously lacking in this case...

14. No it wouldn't. In fact it requires less.
Java 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.

#### Posting Permissions

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