Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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"}

    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]); 
    		
    	}
    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.

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

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

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

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

  4. #4
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Taking values out of one array and putting them into another.

    Yeah, I removed it for the example but I manually called

    Java Code:
    System.arraycopy(list,1,zips,0,1); 
    		System.arraycopy(list,3,zips,1,1);
    and it changed the values. There seems to be something wrong with my for loops. Are they nested properly?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Taking values out of one array and putting them into another.

    Are they nested properly?
    See my previous response:
    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.

  6. #6
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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:
    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]); 
    		
    	}
    	
    	
    	}
    Printout:
    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:
    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.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]); 
    		
    	}
    	
    	
    	}
    Printout
    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

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

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

  8. #8
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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:
    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]
    Code:
    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)); 
    			}
    			
    		}

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

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

  10. #10
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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:

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

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

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

  12. #12
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

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

    Java 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)); 
    		
    	}
    	
    	
    	}
    Here is the output
    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]

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default 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.
    That could indicate that you don't have a solution for the problem. You must get a solution BEFORE you write the code.

    Look at ths part of the print out:
    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
    You need to use the two indexes correctly. Your looping does not work.

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

  14. #14
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

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

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Taking values out of one array and putting them into another.

    In my head the for loop logic works
    You must not be considering all of the details. Using paper and pencil could improve your understanding.
    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.

  16. #16
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Taking values out of one array and putting them into another.

    I will try that. Thank you

  17. #17
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default 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++;
    			
    			 
    		}

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

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

  19. #19
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Taking values out of one array and putting them into another.

    Quote Originally Posted by Norm View Post
    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.
    I figured I might as well use arraycopy. I originally had a simple assignment statement, but they both work. I removed the while loop and it works the same. Thanks for the input.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default Re: Taking values out of one array and putting them into another.

    I figured I might as well use arraycopy.
    That looks like you don't know what you are doing. And it is confusing to read.

Page 1 of 2 12 LastLast

Similar Threads

  1. Values in Array
    By jazob in forum New To Java
    Replies: 2
    Last Post: 12-10-2011, 01:29 AM
  2. taking something from an array.
    By shazakala in forum New To Java
    Replies: 1
    Last Post: 05-10-2011, 01:36 AM
  3. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  4. Replies: 0
    Last Post: 02-28-2009, 11:16 AM
  5. Same values in an array
    By hawaiifiver in forum New To Java
    Replies: 3
    Last Post: 02-24-2009, 08:33 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
  •