Results 1 to 14 of 14
  1. #1
    stegano is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default please help! (array-register)

    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]);
    please help. Thanks

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

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

    Default

    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. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    Quote Originally Posted by stegano View Post
    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. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    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. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    and yet still no question

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

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

  9. #9
    stegano is offline Member
    Join Date
    Feb 2011
    Posts
    28
    Rep Power
    0

    Default

    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. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    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. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

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

  13. #13
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

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

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    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.

Similar Threads

  1. Register content of jTextField
    By jeata in forum New To Java
    Replies: 3
    Last Post: 11-19-2010, 09:22 PM
  2. Oracle Connect thru register driver
    By chyrl in forum JDBC
    Replies: 5
    Last Post: 05-11-2010, 03:45 PM
  3. Oracle connection and register
    By digioleg in forum New To Java
    Replies: 1
    Last Post: 08-19-2009, 07:43 PM
  4. Register a callback function in other class
    By barts2108 in forum New To Java
    Replies: 2
    Last Post: 11-10-2008, 04:24 PM
  5. can't register a MySQL driver
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-03-2008, 11:13 PM

Posting Permissions

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