Results 1 to 9 of 9
  1. #1
    zlich is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Exclamation How to Insert elements into one array

    i have a text message: M 3 r ! a 1 o t 0 v h space e space P s = 16 characters.
    I want to insert this message into an integer array with 20 length, with additional four null = 0 characters.
    Then the result that i want is: M 3 r ! a 1 o null t 0 v null h space e null space P s null = 20 characters.

    I have tried many ways to resolve this problem. Please help me....
    thanks before.

  2. #2
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    So post what you've tried and ask a specific question about your code. We're not mind readers, you know. No one's going to do the whole thing for you.

  3. #3
    zlich is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
            //insert 0
            int nol = (kolom * baris) - tempChiper.length();
            int []temp = new int [tempChiper.length() + nol]; //integer array
            int []pos = new int [nol];
            int j = 0;
            for (int i = 0; i < chiperBlock.length; i++) { // get 0 positions
                if (chiperBlock[i] == 0) {
                    pos[j] = i;   // contain index of null value
                    j++;          
                }
            }
            
            int z = 0;
            for (int l = 0; l < nol; l++){
                for (int i = 0; i < pos[l]; i++) {
                    temp[i] = tempChiper.charAt(i);
                    temp[pos[l]] = 0;
                }
                for (int k = pos[l]; k < pos[l+1]; k++) {
                    temp[k + 1] = tempChiper.charAt(k);                
                    //temp[k] = tempChiper.charAt(k);
                }
                z++;
            }
    Sorry, this is what i have done....

    the question is: how to insert text (temChiper) into an integer array (temp[]) with null value (0) at spesific index (pos[])?
    Last edited by zlich; 01-02-2010 at 11:20 AM.

  4. #4
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    And your question is? Does the code compile? Does it show required output? If not, what's the required output and how does yours differ? What specific parts are you having trouble with?

  5. #5
    zlich is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    how to insert text (temChiper) into an integer array (temp[]) with null value (0) at spesific index (pos[])?
    yes, it is compiled. but the output
    tempPlain: M3r!a1ot0vh e Ps
    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 16

    my required out put is:
    M 3 r ! a 1 o 0 t 0 v 0 h e 0 P s 0

  6. #6
    ojn
    ojn is offline Senior Member
    Join Date
    Aug 2008
    Location
    Stockholm, Sweden
    Posts
    119
    Rep Power
    0

    Default

    The error indicates that the specific index is greater than the size of the array.

    Old string, 14 chars.
    Java Code:
    M3r!a1ot0vhePs
    New (output) string, >14 chars.
    Java Code:
    M3r!a1o0t0v0he0Ps0
    I'm guessing the four 0's are what you want to insert?

    Am I completely misunderstanding you, or do you want to insert new values into an existing array, making it larger?

    Since the code in your last post doesn't compile on its own, I'm unable to run it.

  7. #7
    zlich is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    do you want to insert new values into an existing array, making it larger?
    I want to insert text
    Java Code:
    M3r!a1ot0vhePs
    into an existing integer array (temp[]). and making it larger with insert new values (0).
    and in the array I want to paste the value 0 at a particular index.

    I'm guessing the four 0's are what you want to insert?
    that's right. at a particular index.

    Since the code in your last post doesn't compile on its own, I'm unable to run it.
    the code is a part of my project. so you can't compile it separately.

  8. #8
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    k ..whats kolom and baris?..i can say tempchiper is the string.
    cant help too much becaue complete code is not shown..but i can help.
    Java Code:
    for (int l = 0; l < nol; l++){
                for (int i = 0; i < pos[l]; i++) {
                    temp[i] = tempChiper.charAt(i);
                    temp[pos[l]] = 0;
                }
    over here dnt u think its strange..

    the 1st line inside the 2nd for loop adds the characters to the array.
    but in the 2nd line your replacing them with zeroes.
    partially filled array data structure will act as a perfect data structure over here.

  9. #9
    zlich is offline Member
    Join Date
    Jan 2010
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    int nol = (kolom * baris) - tempChiper.length();
    I use it for array size
    Java Code:
    int []pos = new int [nol];
    I use 2 loops for:
    1. of index 0 found
    Java Code:
    for (int l = 0; l < nol; l++)
    2. to fill an array of values between the index and index +1 for
    Java Code:
    (int i = 0; i < pos[l]; i++)
    you think, how should be?

Similar Threads

  1. Looping Array Elements
    By enzyme in forum New To Java
    Replies: 3
    Last Post: 11-26-2009, 04:35 PM
  2. Printing Array elements
    By new person in forum New To Java
    Replies: 2
    Last Post: 02-23-2009, 08:57 PM
  3. comparing elements in array
    By garyscott101 in forum New To Java
    Replies: 14
    Last Post: 12-10-2008, 03:01 PM
  4. comparing array elements
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-13-2008, 02:33 AM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 PM

Tags for this Thread

Posting Permissions

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