Results 1 to 9 of 9
  1. #1
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Array swap with different type help...

    Hello everyone... I made a selection sort algorithm to sort an array of integers but I also have an array of strings that I want to swap as well whenever there is a swap that occurs... Here is my code:

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    import java.util.*;
    
    public class readFile extends NoSuchElementException {
    
    	public Scanner inputStream;
    	public Scanner inputStreamName;
    	ArraySort arrays= new ArraySort();
    	
    	
    
    	public void openFileID() 
    	{
    		
    		File textFile = new File("C:\\Users\\Dan\\Desktop\\studentId.txt"); 
    		
    		File studentTextFile=new File ("C:\\Users\\Dan\\Desktop\\studentNames.txt");
    
    		inputStream = null; // creates a scanner to read the file
    		inputStreamName=null;
    
    		try // looks for the file
    		{
    			inputStream = new Scanner(textFile);
    			inputStreamName=new Scanner(studentTextFile);
    		} 
    		catch (FileNotFoundException e) // if the file isn't located
    		{
    			System.out.println("File not found");
    		}
    	}
    
    	public void unsortedFile() 
    	{
    		int IDarray[] = new int[9]; //creates array to store input with sizes of 10
    		String Namearray[]=new String[9];
    		int i = 0;
    
    		String linereader = inputStream.nextLine(); //scanner input for each file
    		String namereader=inputStreamName.nextLine();
    		
    		try 
    		{
    			while (linereader!= null && namereader!=null) 
    			{
    				IDarray[i] = Integer.parseInt(linereader); //changes linereader into type int
    				linereader = inputStream.nextLine();
    				Namearray[i]=namereader; //sets namereader values (studentName text input) to variable
    				namereader = inputStreamName.nextLine();
    				i++;
    			}
    		}
    
    		catch (Exception e) 
    		{
    			e.fillInStackTrace();
    		}
    
    		for (int j = 0; j < IDarray.length; j++) 
    		{
    			System.out.print(IDarray[j] + "\t\t"); //reiterates through loop to get individual values
    			System.out.println(Namearray[j]);
    		}
    		
    [B]		System.out.println();
    		System.out.println("The sorted ID array is: ");
    		System.out.println();
    		ArraySort.selectionSort(IDarray);
    		
    			for (int k=0; k<IDarray.length; k++)
    			{
    				System.out.println(IDarray[k] + "\t\t" + Namearray[k]);
    			}
    			System.out.println();
    	}[/B]
    
    }
    The bold is my second loop that is supposed to sort out both the ID and have the name, age, and ID stay with the same name. The next class is my selection sort

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.*;
    
    
    public class ArraySort 
    {
    	
    	public static void selectionSort(int[] anArray)
    	{
    		for (int index=0; index< anArray.length-1; index++)
    		{
    			int indexOfNextSmallest=getIndexOfSmallest(index, anArray);
    			interchange(index, indexOfNextSmallest, anArray);
    		}
    	}
    	
    	private static int getIndexOfSmallest(int startIndex, int[] a)
    	{
    		int min=a[startIndex];
    		int indexOfMin = startIndex;
    		for (int index = startIndex + 1; index<a.length; index++)
    		{
    			if (a[index] < min)
    			{
    				min=a[index];	
    				indexOfMin=index;
    			}
    		}
    		
    		return indexOfMin;
    		
    	}
    	
    	private static void interchange (int i, int j, int[]a)
    	{
    		int temp=a[i];
    		a[i]=a[j];
    		a[j]=temp;
    		
    	}
    }
    Here is my output:

    The unassorted text file is as follows :

    Student ID Name Age GPA
    735491 Daniel 23 3.10
    458753 Mary 22 2.98
    458756 Josh 19 2.93
    456875 Robert 20 2.09
    456876 Matt 22 2.94
    876594 Alex 23 3.29
    876543 Nick 24 3.00
    687452 Rosie 20 2.39
    987657 Max 22 2.48

    The sorted ID array is:

    456875 Daniel 23 3.10
    456876 Mary 22 2.98
    458753 Josh 19 2.93
    458756 Robert 20 2.09
    687452 Matt 22 2.94
    735491 Alex 23 3.29
    876543 Nick 24 3.00
    876594 Rosie 20 2.39
    987657 Max 22 2.48



    As you can see, my IDs are ordered but I want the name age and GPA columns to switch with the corresponding IDs...anyone have any ideas or can possibly point me in the right direction? The previous help on this forum helped me to get this far and this is the very last thing I have to do... please help!!! thanks in advance.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Array swap with different type help...

    If you are sorting some parallel arrays based on the contents of one of the arrays, then every thing you do to one array you must do exactly the same to the other arrays to keep them in sync.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Re: Array swap with different type help...

    Quote Originally Posted by Norm View Post
    If you are sorting some parallel arrays based on the contents of one of the arrays, then every thing you do to one array you must do exactly the same to the other arrays to keep them in sync.
    The only problem is for one array it is of type int, and the other array is of type string. My selection sort only works for integers so how can I possibly have the string array mimic the int array ? Thanks again though for the tip Norm

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Array swap with different type help...

    The code needs to be rewritten to handle parallel arrays. The contents of the other arrays (not being sorted) is not important. Swapping two elements in an array uses the same order of statements no matter what type of data is in the array. A difference is what type the temp variable is.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Re: Array swap with different type help...

    Quote Originally Posted by Norm View Post
    The code needs to be rewritten to handle parallel arrays. The contents of the other arrays (not being sorted) is not important. Swapping two elements in an array uses the same order of statements no matter what type of data is in the array. A difference is what type the temp variable is.
    ouch...thought that was what someone would say...thanks again Norm back to the drawing board

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: Array swap with different type help...

    I wrote a blog article on sorting once; the article describes a heap sort algorithm that can solve exactly your problem; note that there is no free lunch though: you still have to be able to swap elements in different arrays; the sorting algorith conducts which elements to swap. See the link near the top right corner of this reply.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    dd09wrx is offline Member
    Join Date
    Nov 2013
    Posts
    7
    Rep Power
    0

    Default Re: Array swap with different type help...

    store all of the persons data in a map so you have the persons full data. store the maps for all the people in a list. once you have got the order of Integer or String then you can do something like this:

    //Map List variable is called list in this example

    for(int i=0;i<idarray;i++){
    for(int j=0;j<idarray;j++){
    if(list.get(j).get("id").equals(idarray.get(i))){
    System.out.println(list.get(j).get("id"));System.o ut.print(list.get(j).get("name"));System.out.print (list.get(j).get("age"));System.out.print(list.get (j).get("GPA"));
    }
    }
    }

    Also if you stored the sortable data in lists and not arrays you can you collection method to automatically sort the data and save reinventin the wheel!

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: Array swap with different type help...

    Of course another possibility would be to declare a class to hold the various data types. Then have an array of that class. When you sort on one particular element of the data type, you just need to swap the class references as appropriate. This also allows you to add more state to the class without changing the sort algorithm.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,814
    Rep Power
    25

    Default Re: Array swap with different type help...

    I suspect that the assignment is to use parallel arrays, not classes. The next lesson will have classes so the student will see how much better classes are than arrays.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. How to swap the first and last array value
    By ecc.soccer in forum New To Java
    Replies: 5
    Last Post: 10-29-2013, 04:20 PM
  2. Swap 2 dimensional Array
    By whit3ang3l in forum New To Java
    Replies: 4
    Last Post: 03-06-2012, 01:53 PM
  3. Swap array value
    By irnie1994 in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 06:06 PM
  4. how can i loop my array swap
    By belfast09 in forum New To Java
    Replies: 3
    Last Post: 06-15-2011, 05:35 AM
  5. Multidimensional array - swap
    By BeginnerNoob in forum New To Java
    Replies: 13
    Last Post: 03-30-2011, 04:02 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
  •