# Thread: How to Insert elements into one array

1. Member
Join Date
Jan 2010
Posts
5
Rep Power
0

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

thanks before.

2. Senior Member
Join Date
Aug 2008
Location
Stockholm, Sweden
Posts
119
Rep Power
0
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. Member
Join Date
Jan 2010
Posts
5
Rep Power
0
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 12:20 PM.

4. Senior Member
Join Date
Aug 2008
Location
Stockholm, Sweden
Posts
119
Rep Power
0
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. Member
Join Date
Jan 2010
Posts
5
Rep Power
0
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. Senior Member
Join Date
Aug 2008
Location
Stockholm, Sweden
Posts
119
Rep Power
0
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. Member
Join Date
Jan 2010
Posts
5
Rep Power
0
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. Senior Member
Join Date
Oct 2009
Location
California,US
Posts
201
Rep Power
8
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. Member
Join Date
Jan 2010
Posts
5
Rep Power
0
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?