Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with parallel array

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with parallel array

    /*
    Program created by: Craig Noguez
    	A program that creates a file containing a table showing the student ID numbers and averages of
    	all the students in a course in ascending order by ID number.
    */
     
    import java.io.*;
    import java.util.Scanner;
     
    public class assignment6
    {
    	public static void main(String[] args) throws IOException
    	{
    		Scanner keyboard = new Scanner(System.in);
    		String fileName = "";
    		String[] idnumbers;
    		String[] array;
    		String[] average;
    		int index = 0;
    		String[] test = new String[100];
     
    		System.out.println("Enter the name of the file you would like to read.");
    		fileName = keyboard.nextLine();
     
    		System.out.println();
     
    		idnumbers = getid(fileName);
    		average = getavg(fileName);			
    		selectionSort(idnumbers);
    		System.out.println("Enter the name of the file you would like to save the data to.");
    		fileName = keyboard.nextLine();
     
    		PrintWriter outputFile = new PrintWriter(fileName);
    		outputFile.printf(" IdNumber						Average\n");
    		for(index = 0; index < average.length; index++)
    			if(average[index] == null)
    				outputFile.close();
    			else
    				outputFile.printf("%5s %23s\n", idnumbers[index], average[index]);
    	}
     
    	/**
    		A method to get the id-number from a text file.
     
    		@param fileName  The file to get the id-numbers from
    		@return idnumbers  Returns the array with the idnumbers stored
    	*/
     
    	public static String[] getid(String fileName) throws IOException
    	{
    		File file = new File(fileName);
    		Scanner inputFile = new Scanner(file);
    		int index = 0;
    		int startid = 2;
    		String[] idnumbers = new String[100];
    		String[] WholeFile = new String[100];
     
    		while(inputFile.hasNext() && index < idnumbers.length)
    		{
    			WholeFile[index] = inputFile.nextLine();
    			index++;
    		}
     
    		for(index = 0; startid < WholeFile.length; index++)
    		{
    			idnumbers[index] = WholeFile[startid];
    			startid += 4;
    		}
    		return idnumbers;
    	}
     
    	/**
    		A method to get the averages from a text file.
     
    		@param fileName The file to get the averages from
    		@return average  Returns an array with the averages stored in it
    	*/
     
    	public static String[] getavg(String fileName) throws IOException
    	{
    		File file = new File(fileName);
    		Scanner inputFile = new Scanner(file);
    		int index = 0;
    		int startavg = 3;
    		String[] WholeFile = new String[100];
    		String[] average = new String[100];
     
    		while(inputFile.hasNext() && index < average.length)
    		{
    			WholeFile[index] = inputFile.nextLine();
    			index++;
    		}
     
    		for(index = 0; startavg < WholeFile.length; index++)
    		{
    			average[index] = WholeFile[startavg];
    			startavg += 4;
    		}
    		return average;
    	}
     
    	/**
    		A method that sorts the inputed string into alphabetical order.
     
    		@param array  The string to be sorted.
    	*/
    	public static void selectionSort(String[] array)
    	{
    		int startScan, index, minIndex;
    		String minValue;
     
    		for (startScan = 0; startScan < (array.length-1); startScan++)
    		{
    			minIndex = startScan;
    			minValue = array[startScan];
    			for(index = startScan + 1; index < array.length; index++)
    			{
    				if (array[index] == null)
    					break;
    				else if (array[index].compareTo(minValue) < 0)
    				{
    					minValue = array[index];
    					minIndex = index;
    				}
    			}
    			array[minIndex] = array[startScan];
    			array[startScan] = minValue;
    		}
    	}
    }
    I need my id number to get sorted along with the average but I have no idea how to do it and my teacher barely touched on parallel arrays help please thanks!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with parallel array

    Parallel arrays are evil. What you should do instead is create a Student class that holds the ID and score of each Student. Then you can have a single array (or List) to hold all the Student objects.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with parallel array

    Ya see thats easy ive looked at it and it would make life so much simpler but my teacher is a nazi and she wont let us use it

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Help with parallel array

    If your instruction state that you must use parallel arrays then what you have to do is each time you swap/move values in one array make the same swap/move in the other array.
    Improving the world one idiot at a time!

Similar Threads

  1. Help with code dealing with parallel arrays.
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2011, 07:43 PM
  2. parallel array newbie
    By NewAtJava in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 1st, 2011, 09:21 PM
  3. problem searching for a String element in a parallel array
    By david185000 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2010, 01:24 PM
  4. problem for writing to parallel port LPT1 on windows by RXTX library
    By sahar_m in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 20th, 2010, 04:31 AM
  5. theory about serial / parallel port & java
    By wolfgar in forum Java Theory & Questions
    Replies: 5
    Last Post: January 4th, 2010, 10:08 PM

Tags for this Thread