Results 1 to 9 of 9
- 01-02-2010, 10:58 AM #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.
I have tried many ways to resolve this problem. Please help me....
thanks before.
- 01-02-2010, 11:06 AM #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.
- 01-02-2010, 11:15 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
Sorry, this is what i have done....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++; }
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.
- 01-02-2010, 11:19 AM #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?
- 01-02-2010, 11:27 AM #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
- 01-02-2010, 12:08 PM #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.
New (output) string, >14 chars.Java Code:M3r!a1ot0vhePs
I'm guessing the four 0's are what you want to insert?Java Code:M3r!a1o0t0v0he0Ps0
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.
- 01-02-2010, 01:20 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
I want to insert textdo you want to insert new values into an existing array, making it larger?
into an existing integer array (temp[]). and making it larger with insert new values (0).Java Code:M3r!a1ot0vhePs
and in the array I want to paste the value 0 at a particular index.
that's right. at a particular index.I'm guessing the four 0's are what you want to insert?
the code is a part of my project. so you can't compile it separately.Since the code in your last post doesn't compile on its own, I'm unable to run it.
- 01-02-2010, 02:03 PM #8
Senior Member
- Join Date
- Oct 2009
- Location
- California,US
- Posts
- 201
- Rep Power
- 4
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.
over here dnt u think its strange..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; }
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.
- 01-02-2010, 02:37 PM #9
Member
- Join Date
- Jan 2010
- Posts
- 5
- Rep Power
- 0
I use it for array sizeJava Code:int nol = (kolom * baris) - tempChiper.length();
I use 2 loops for:Java Code:int []pos = new int [nol];
1. of index 0 found2. to fill an array of values between the index and index +1 forJava Code:for (int l = 0; l < nol; l++)
you think, how should be?Java Code:(int i = 0; i < pos[l]; i++)
Similar Threads
-
Looping Array Elements
By enzyme in forum New To JavaReplies: 3Last Post: 11-26-2009, 04:35 PM -
Printing Array elements
By new person in forum New To JavaReplies: 2Last Post: 02-23-2009, 08:57 PM -
comparing elements in array
By garyscott101 in forum New To JavaReplies: 14Last Post: 12-10-2008, 03:01 PM -
comparing array elements
By Jeremy720 in forum New To JavaReplies: 2Last Post: 10-13-2008, 02:33 AM -
Help with array of elements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 05:33 PM


LinkBack URL
About LinkBacks

Bookmarks