Results 1 to 20 of 23
- 02-04-2012, 11:43 PM #1
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Taking values out of one array and putting them into another.
Hello, as part of a project I am working on I have pairs of names and zip codes stored in an array. I need to sort the zip codes in numerical order and am taking them out and putting them into another array to do that. The problem is that the code I have written is not taking the proper values.
The value of the list array is below:
list[] = {"Bryan", "11787", "Thomas", "11759", "Dillon" ,"123456"}
I am trying to send the zip codes to the array zips[], but only the last zip code(123456) is making it there for all index values. Any help is appreciated.Java Code:public static void sort(String[] list) { int j; int a; String[] zips = new String[3]; for(j=1; j<=5; j+=2) { for(a=0; a <=2; a++) { zips[a] = list[j]; } } //for (int s = 0; s <= 2; s++) //{ // System.out.println(zips[s]); //} System.out.println(zips[0]); System.out.println(zips[1]); System.out.println(zips[2]); }
- 02-05-2012, 01:43 AM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Taking values out of one array and putting them into another.
I replace a portion of that code with System.arraycopy(), but the values are still wrong. It should be printing out 1,3,5 but it is printing out all 5s.
Java Code:public static void sort(String[] list) { int pos_list; int pos_zips; String[] zips = new String[3]; for(pos_list=1;pos_list<=5;pos_list=pos_list+2) { for(pos_zips=0;pos_zips<=2;pos_zips++) { System.arraycopy(list,pos_list,zips,pos_zips,1); } } System.out.println(zips[0]); System.out.println(zips[1]); System.out.println(zips[2]); }Last edited by Wnt2bsleepin; 02-05-2012 at 01:55 AM.
- 02-05-2012, 02:22 AM #3
Re: Taking values out of one array and putting them into another.
Are you sure you are using the arraycopy method correctly? Have you tried it with a simple program to make sure you get the output you want?
When working on these kinds of problems you need to print out the value of all the arguments to the arrayCopy method and the results of each call to the method to see what is happening. The print out should show you where the problem is.
Use the Arrays toString() method to format the array for printing.
- 02-05-2012, 02:57 AM #4
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Taking values out of one array and putting them into another.
Yeah, I removed it for the example but I manually called
and it changed the values. There seems to be something wrong with my for loops. Are they nested properly?Java Code:System.arraycopy(list,1,zips,0,1); System.arraycopy(list,3,zips,1,1);
- 02-05-2012, 03:02 AM #5
Re: Taking values out of one array and putting them into another.
See my previous response:Are they nested properly?
When working on these kinds of problems you need to print out the value of all the arguments to the arrayCopy method and the results of each call to the method to see what is happening. The print out should show you where the problem is.
Use the Arrays toString() method to format the array for printing.
- 02-05-2012, 03:25 AM #6
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Taking values out of one array and putting them into another.
Forgive me if I didn't understand what you're saying, but you wanted me to print out the variables correct? I also posted the entire code.
Here is the print out where I implemented my original solution:
Code:
Printout:Java Code:import java.io.*; public class Zipcodes { public static void main(String[] args) throws IOException { String[] pairs = new String[6]; System.out.println("Please enter name followed by the zip code. You may enter up to 25 pairs."); BufferedReader in; in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter them in the format:"); for (int i=0; i <= 5; i++) { pairs[i] = in.readLine(); } sort(pairs); } public static void sort(String[] list) { int pos_list; int pos_zips; String[] zips = new String[3]; for(pos_list=1;pos_list<=5;pos_list=pos_list+2) { for(pos_zips=0;pos_zips<=2;pos_zips++) { System.arraycopy(list,pos_list,zips,pos_zips,1); } } //System.arraycopy(list,1,zips,0,1); //System.arraycopy(list,3,zips,1,1); System.out.println("list[0] " + list[0]); System.out.println("list[1] " + list[1]); System.out.println("list[2] " + list[2]); System.out.println("list[3] " + list[3]); System.out.println("list[4] " + list[4] ); System.out.println("list[5] " + list[5]); System.out.println("zips[0] " + zips[0]); System.out.println("zips[1] " + zips[1]); System.out.println("zips[2] " + zips[2]); } }
Java Code:list[0] 0 list[1] 1 list[2] 2 list[3] 3 list[4] 4 list[5] 5 zips[0] 5 zips[1] 5 zips[2] 5
The second one is where I bypass the for loop and manually use the arraycopy method.
Code:
PrintoutJava Code:import java.io.*; public class Zipcodes { public static void main(String[] args) throws IOException { String[] pairs = new String[6]; System.out.println("Please enter name followed by the zip code. You may enter up to 25 pairs."); BufferedReader in; in = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter them in the format:"); for (int i=0; i <= 5; i++) { pairs[i] = in.readLine(); } sort(pairs); } public static void sort(String[] list) { int pos_list; int pos_zips; String[] zips = new String[3]; //for(pos_list=1;pos_list<=5;pos_list=pos_list+2) //{ // for(pos_zips=0;pos_zips<=2;pos_zips++) //{ //System.arraycopy(list,pos_list,zips,pos_zips,1); //} //} System.arraycopy(list,1,zips,0,1); System.arraycopy(list,3,zips,1,1); System.arraycopy(list,5,zips,2,1); System.out.println("list[0] " + list[0]); System.out.println("list[1] " + list[1]); System.out.println("list[2] " + list[2]); System.out.println("list[3] " + list[3]); System.out.println("list[4] " + list[4] ); System.out.println("list[5] " + list[5]); System.out.println("zips[0] " + zips[0]); System.out.println("zips[1] " + zips[1]); System.out.println("zips[2] " + zips[2]); } }
Java Code:list[0] 0 list[1] 1 list[2] 2 list[3] 3 list[4] 4 list[5] 5 zips[0] 1 zips[1] 3 zips[2] 5
- 02-05-2012, 03:41 AM #7
Re: Taking values out of one array and putting them into another.
Your printlns should be inside of the loop about line 47 and show what the loop does every time it goes around.
The print outs on lines 57 to 69 are useless for seeing what the code is doing.
You already know what the results are. You need to see how the program is doing what it is doing.
Use the Arrays toString() method to format the array for printing:
System.out.println(Arrays.toString(THEARRAYNAMEHER E));
This is a lot of work for nothing:
System.arraycopy(list,1,zips,0,1);
vs
zips[0] = list[1];Last edited by Norm; 02-05-2012 at 03:43 AM.
- 02-05-2012, 05:37 AM #8
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Taking values out of one array and putting them into another.
Wow, that made it a little clearer as to what the for loop is doing. Thank you. I am having a hard time interpreting it though. Seems like it's doing too many iterations.
Printout:
Code:Java Code:[1, null, null] [1, 1, null] [1, 1, 1] [3, 1, 1] [3, 3, 1] [3, 3, 3] [5, 3, 3] [5, 5, 3] [5, 5, 5]
Java Code:for(pos_list=1;pos_list<=5;pos_list=pos_list+2) { for(pos_zips=0;pos_zips<=2;pos_zips++) { System.arraycopy(list,pos_list,zips,pos_zips,1); System.out.println(Arrays.toString(zips)); } }
- 02-05-2012, 01:39 PM #9
Re: Taking values out of one array and putting them into another.
You should also print out the values of the source index and the target indexes for each character that is copied.
That should show you how you are using the indexes.
Take a piece of paper and draw the two arrays and the indexes for each array. Then look at how the values of the indexes change as you copy a character from one array to the other.
- 02-05-2012, 07:28 PM #10
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Taking values out of one array and putting them into another.
I don't understand. I printed out the arrays before the for loop, during the for loop and after the for loop. I purposely am copying the numbers that correlate to the index position. For example, the number 5 below is the index position 5 or zips[5]. I did this to make diagnosing things like this easier. I am trying to copy every second number from the first array into the second, in this case the numbers 1,3 and 5. The condition of the arrays during each iteration of the loop is:
but the loop is going for too many iterations. It should only have to do this three times. In my head the logic makes sense, so I am missing something here and am lost.Java Code:[1, null, null] [1, 1, null] [1, 1, 1] [3, 1, 1] [3, 3, 1] [3, 3, 3] [5, 3, 3] [5, 5, 3] [5, 5, 5]
- 02-05-2012, 07:44 PM #11
Re: Taking values out of one array and putting them into another.
You should also print out the values of the source index and the target indexes for each character that is copied.
That should show you how you are using the indexes. What are the values of pos_list and pos_zips as the loops execute?
You need to consider how you want the elements copied and how the indexes need to change to do that.
Did you do as I suggested:
Take a piece of paper and draw the two arrays and the indexes for each array. Then look at how the values of the indexes change as you copy a character from one array to the other.
- 02-05-2012, 08:05 PM #12
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Taking values out of one array and putting them into another.
I did the paper thing and it keeps shifting the indexes too much.
Here is the outputJava Code:public static void sort(String[] list) { int pos_list; int pos_zips; String[] zips = new String[3]; System.out.println("list array: " + Arrays.toString(list)); System.out.println("Zips before for loop " + Arrays.toString(zips)); for(pos_list=1;pos_list<=5;pos_list+=2) { System.out.println("pos_list: " + pos_list); for(pos_zips=0;pos_zips<=2;pos_zips++) { System.out.println("pos_zips: " + pos_zips); System.arraycopy(list,pos_list,zips,pos_zips,1); System.out.println(Arrays.toString(zips)); } } System.out.println("zips after for loop: " + Arrays.toString(zips)); } }
Java Code:list array: [0, 1, 2, 3, 4, 5] Zips before for loop [null, null, null] pos_list: 1 pos_zips: 0 [1, null, null] pos_zips: 1 [1, 1, null] pos_zips: 2 [1, 1, 1] pos_list: 3 pos_zips: 0 [3, 1, 1] pos_zips: 1 [3, 3, 1] pos_zips: 2 [3, 3, 3] pos_list: 5 pos_zips: 0 [5, 3, 3] pos_zips: 1 [5, 5, 3] pos_zips: 2 [5, 5, 5] zips after for loop: [5, 5, 5]
- 02-05-2012, 08:14 PM #13
Re: Taking values out of one array and putting them into another.
That could indicate that you don't have a solution for the problem. You must get a solution BEFORE you write the code.I did the paper thing and it keeps shifting the indexes too much.
Look at ths part of the print out:
You need to use the two indexes correctly. Your looping does not work.pos_list: 5 <<<<<<<<<<<Sets the source index for all of the following copies
pos_zips: 0 <<<<<<<<<<<set first target index
[5, 3, 3] <<<<<<<<<< results of first copy
pos_zips: 1
[5, 5, 3]
pos_zips: 2
--------------------------------------------------------------------------------
Another approach:
Forget about using for loops, work out the logic using a while loop.Last edited by Norm; 02-05-2012 at 08:42 PM.
- 02-05-2012, 08:49 PM #14
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Taking values out of one array and putting them into another.
I don't get it. In my head the for loop logic works, but clearly it does not. I have no idea how else I would take the strings and put them into another array.
- 02-05-2012, 09:03 PM #15
Re: Taking values out of one array and putting them into another.
You must not be considering all of the details. Using paper and pencil could improve your understanding.In my head the for loop logic works
Also Look at what was printed out.
Did you see this from my last post:
Another approach:
Forget about using for loops, work out the logic using a while loop.
- 02-05-2012, 09:06 PM #16
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Taking values out of one array and putting them into another.
I will try that. Thank you
- 02-05-2012, 09:13 PM #17
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Taking values out of one array and putting them into another.
I did it. Thank you for all your help. A simple while loop helped alleviate the problem.
Java Code:for(pos_list=1;pos_list<=5;pos_list+=2) { //System.out.println("pos_list: " + pos_list); while(pos_list <= 5) { System.out.println("pos_list: " + pos_list); System.out.println("pos_zips: " + pos_zips); System.arraycopy(list,pos_list,zips,pos_zips,1); System.out.println(Arrays.toString(zips)); break; } pos_zips++; }
- 02-05-2012, 09:40 PM #18
Re: Taking values out of one array and putting them into another.
Why are you using arraycopy for one element instead of a simple assignment statement?
If you always break out of the while loop, it never loops and is a waste of time. It could be removed.
- 02-05-2012, 10:29 PM #19
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
- 02-05-2012, 10:35 PM #20
Similar Threads
-
Values in Array
By jazob in forum New To JavaReplies: 2Last Post: 12-10-2011, 01:29 AM -
taking something from an array.
By shazakala in forum New To JavaReplies: 1Last Post: 05-10-2011, 01:36 AM -
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 02:36 PM -
how to insert data in table (stored proc) without taking all the values as parameters
By Faheem_Ahmed in forum New To JavaReplies: 0Last Post: 02-28-2009, 11:16 AM -
Same values in an array
By hawaiifiver in forum New To JavaReplies: 3Last Post: 02-24-2009, 08:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks