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

    Default Sort through array, finding pairs.

    I am writing a program that takes zip codes and associated names, and put them in numerical order. I do this by putting the data into an array as Strings. I then remove the zip codes, convert them to ints and put them into their own
    array. The problem is printing them out and still having the associated name being with the proper zip code. Here is the code I have to cyle through three arrays, the zip codes (String), the names(String) and the reference array which
    contains the original data.

    String[] zipcodestring (11759, 11787,12345)
    String[] b = (Alpha, Omega, Theta)
    String[] c = (Alpha, 11787, Omega, 11759, Theta, 12345)
    Java Code:
    public static void combiner(int[] a, String[] b, String[] c)
    {
    	
    	String[] zipcodestring = new String[3]; 
    	int d; 
    	String[] finalstring = new String[6]; 
    	Boolean checked=true; 
    	
    	for(d=0;d<a.length;d++)
    	{
    		zipcodestring[d]=Integer.toString(a[d]); 
    	}
    	
    	
    	
    while(checked)	
    {	
    	int x=0; 
    	int z=0; 
    	int i=0;
    	int j=0;
    	String[] temp = new String[1]; 
    	
    	if(b[j].equals(c[x]) && zipcodestring[z].equals(c[x+1]) )
    	{
    		
    		
    		
    			finalstring[j] = b[x];
    			finalstring[j+1] = zipcodestring[x]; 
    			z++; 
    			j++; 
    			x++; 
    			System.out.println("Passed");
    			
    			 
    		
    	}
    	
    	else
    	{
    	temp[x] = zipcodestring[x]; 
    	zipcodestring[x] = zipcodestring[x+1]; 
    	zipcodestring[x+1] = temp[x]; 
    	System.out.println("failed");
    	
    	}
    		
    checked = false; 
    	 
    }
    
    
    
    
    System.out.println(Arrays.toString(finalstring)); 
    
    
    
    
    
    	}
    If anyone would like to see the code in its entirety, I can give it to you. Thank you for any help.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: Sort through array, finding pairs.

    problem is printing them out and still having the associated name being with the proper zip code.
    If you have parallel arrays, how are you keeping them in synch so that the values at any index are associated?

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

    Default Re: Sort through array, finding pairs.

    I am using the C array as a reference for what name is paired with what zip code. Is that what you mean?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: Sort through array, finding pairs.

    Why are you using arrays instead of putting all the data into a class and sorting an array of class objects?

    Do you understand what I mean by parallel arrays? The data at any index in one array is associated with data at the same index in another array. If you move the contents of one array around, you must move the contents of all the array in exactly the same way to keep them in synch.

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

    Default Re: Sort through array, finding pairs.

    I did not know about parallel arrays. I have never done this much data manipulation before and am unsure of how to put it into classes or keep the 2 arrays in sync. I was trying to organize them based on pairs and then stick them into another array. That is the array I planned on printing.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: Sort through array, finding pairs.

    Can you explain how you are sorting the data and what you want to get as a result?
    What is the input to the program and what output are you trying to get?

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

    Default Re: Sort through array, finding pairs.

    Sure. The goal of the project is to have a user enter 25 pairs of names and zip codes. For instance, Alpha 11787. It then has to print out the zip codes in numerical order of least to greatest. It has to print out the names with them, in a table format. I plan on
    using printf to do that.

    Java Code:
    Input: Alpha<Enter>
             11787 <Enter>
             Omega<Enter>
             11759 <Enter>
             Theta<Enter>
             12345 <Enter>
    
    Output:
           Alpha       11759
           Omega       11787
           Theta        12345
    I am sorting the data the best way that I could. The data comes in as an array (Alpha,11787,Omega,11759,Theta,12345), it is then has the zip codes taken out and converted into integers. These are put in a separate array. They are then put in numerical
    order and converted back into strings. They are then supposed to be matched up with the names that they originally came in with, using the original array as a reference. So, Alpha is associated with 11787. Omega 11759 and Theta 123456. They are paired
    and then put into another final string array to be printed out. That was my plan.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: Sort through array, finding pairs.

    Sounds like parallel arrays would work, if you are not going to use classes to hold the data.
    Fill two arrays, one with the zip and the other with the name.
    When you move an element around in one array, move the element at the same index the same way in the other array.

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

    Default Re: Sort through array, finding pairs.

    How do I move the names around the same as the zip codes? I am using the sort() method that is predefined, so I don't know how it move around the items in the array.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: Sort through array, finding pairs.

    Parallel arrays won't work it you don't do the sort yourself.
    Build a String with the sort field at the front: 11759Alpha
    and sort that.

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

    Default Re: Sort through array, finding pairs.

    I would need to rework a large part of my code to do that. You helped me with it a day or so ago. I am afraid of losing any progress. I can make another class if that would require less "rewiring". How does the class thing work?

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

    Default Re: Sort through array, finding pairs.

    Quote Originally Posted by Norm View Post
    Parallel arrays won't work it you don't do the sort yourself.
    Build a String with the sort field at the front: 11759Alpha
    and sort that.
    If I were to go with that, how would I sort them if they are both strings? One string value can't be greater than another one.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: Sort through array, finding pairs.

    One string value can't be greater than another one.
    Why not? Try it and see what happens.

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

    Default Re: Sort through array, finding pairs.

    I just tried it with a simple experiment.

    Java Code:
    public class stringsort {
    	
    	public static void main(String[] args)
    	{
    		String[] test = new String[3];
    		test[0] = "1";
    		test[1] = "2";
    		test[2] ="3"; 
    	 
    		if(test[0] < test[1] )
    		{
    			
    		}
    	
    	}
    
    }
    You can't apply that kind of operator to a string.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: Sort through array, finding pairs.

    Is that how you are going to sort the arrays?
    What class and method are you going to use to do the sorting?
    I am using the sort() method that is predefined,
    For objects you use the equals() or other methods like compareTo depending on the class.
    Last edited by Norm; 02-07-2012 at 03:38 AM.

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

    Default Re: Sort through array, finding pairs.

    I only learned so far to use the equals() method for strings. I used Arrays.sort() to put the integers in order.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: Sort through array, finding pairs.

    Read the API doc for the Arrays class's many different sort() methods.

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

    Default Re: Sort through array, finding pairs.

    The two that I see that could be useful are sort(Object[] a) and sort(T[] a, Comparator<? super T> c) but I still would like to look into putting the values of the array into a class. How would I go about doing that?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: Sort through array, finding pairs.

    You need to read up on Creating and using classes in an OOP language:
    Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

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

    Default Re: Sort through array, finding pairs.

    I decided to sort the zip codes manually with a bubble sort. I now know how the numbers are rearranged. How would I move the Strings in the same fashion?

    Java Code:
    public static void bubblesort(int[] a)
    	{
    		boolean swapped = true;
    		int temp;
    		while(swapped)
    		{
    		swapped=false; 
    		for(int i=0; i<a.length-1;i++)
    		{
    			 
    			if(a[i] > a[i+1])
    			{
    				temp = a[i];
    				a[i] = a[i+1];
    				a[i+1] = temp; 
    				swapped = true;
    			}
    			 
    		}
    		
    	}	
    		System.out.println("Zips sorted:" + Arrays.toString(a)); 
    	}

Page 1 of 2 12 LastLast

Similar Threads

  1. Need help with Array.sort()
    By Kinney.j in forum New To Java
    Replies: 1
    Last Post: 10-16-2011, 06:51 AM
  2. Algorithm for finding max and min in an array.
    By Shyamz1 in forum New To Java
    Replies: 4
    Last Post: 10-04-2011, 10:45 PM
  3. Finding the Mode in An Array
    By carlodelmundo in forum New To Java
    Replies: 23
    Last Post: 10-31-2010, 12:44 PM
  4. sort array >> need help
    By hongi in forum New To Java
    Replies: 4
    Last Post: 04-25-2010, 09:37 PM
  5. Finding a the max value of the array using a for loop
    By soccer_kid_6 in forum New To Java
    Replies: 1
    Last Post: 04-11-2010, 11:25 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
  •