Java Code Help!!!

Tulip_Doll

Member
Feb 6, 2015
208
13
0
Florida
Here is the Question:

The international Olympics Committee has asked you to write a program to process the data and determine the medal winners for the pairs figure skating. You will be given the following file:
Pairs.txt

Which contains the data for each pair of skaters. The data consists of each skater’s name, their country and the score from each of eight judges on the technical aspects and on the performance aspects. A typical record would be as follows:

Smith
Jones
Australia
5.0 4.9 5.1 5.2 5.0 5.1 5.2 4.8
4.3 4.7 4.8 4.9 4.6 4.8 4.9 4.5

The final score for each skater is the sum of the average of the two categories of score.

Design a class to hold the above data and the final score. Read the data in from the file and calculate the final score for each pair. Sort the array of objects , and display the results on the screen in order, giving special prominence to the medal winners.

Here is my Class:


Code:
public class Skaters {

	private String name1;
	private String name2;
	private String country;
	private double [] arrTech = new double [8];
	private double [] arrArt = new double [8];
	private double score;
	
	
public Skaters (String n1, String n2, String c1, double [] arrT, double [] arrA) 

{
	this.name1 = n1;
	this.name2 = n2;
	this.country = c1;
	this.arrTech = arrT;
	this.arrArt = arrA;
	
	
}
	public void setName1(String n1)
	
	{
		name1 = n1;
	}
	
	public void setName2(String n2)
	
	{
		name2 = n2;
	}
	
	public void setCountry(String c1)
	
	{
		country = c1;
	}

	public void setArrTech (double [] arrT)
	
	{
		arrTech = arrT; 
	}
	
	public void setArrArt(double [] arrA)
	
	{
		arrArt = arrA;
	}
	
	public String getName1()
	
	{
		return name1;
	}
	
	public String getName2()
	
	{
		return name2;
	}
	
	public String getCountry()
	
	{
		return country;
	}
	
	
	public double getScore()
	
	{
		double sum = 0, avg1, avg2;
		
			for (int r = 0; r < arrTech.length; r++)
				
				sum += arrTech[r];
			
					avg1 = sum/arrTech.length;
					
						sum = 0;
				
			for (int s = 0; s < arrArt.length; s++)
				
				sum += arrArt[s];
			
					avg2 = sum/arrArt.length;
					
				return (avg1 + avg2);
	}
	
	
	
	
}

This is my main:

Code:
import java.io.*;
import java.util.Scanner;
public class testSkaters {

	public static void main(String[] args) throws IOException { 
		
		Skaters [] arrSkaters = new Skaters [50]; 
		
		File myFile = new File ("Pairs.txt");
		
		Scanner input = new Scanner (myFile);
		
		int countSkaters = 0;
		String name1;
		String name2;
		String country;
		double [] arr1 = new double [8];
		double [] arr2 = new double [8];
		
		
			while (input.hasNext())
				
				{
				
				name1 = input.nextLine();
				name2 = input.nextLine();
				country = input.nextLine();
				
			for (int i = 0; i < arr1.length; i++)
				
				arr1[i] = input.nextDouble();
			
			for (int j =0; j < arr2.length; j++)
				
				arr2[j] = input.nextDouble();
						
			
				Skaters s1 = new Skaters (name1, name2, country, arr1, arr2);
		
				arrSkaters[countSkaters] = s1; 
		
				countSkaters++;
				
			input.nextLine();	
			
			}
						

		
			for (int t = 0; t < countSkaters; t++)
					
				{	
					System.out.println("Name 1: " + arrSkaters[t].getName1());
				
					System.out.println("Name 2: " + arrSkaters[t].getName2());
					
					System.out.println("Country: " + arrSkaters[t].getCountry());
					
				 printArray(arr1);

				}    
					
					
				}
	

			public static void printArray (double [] arr3)
			{
		        for (int r = 0; r < arr3.length;r++)
		   
		            System.out.print(arr3[r]);
		        System.out.println("");

			
}

}

This thing is when i print it from the file, it prints the same score for everyone but its different in the file.

This is how its in the file

Smith
Jones
Australia
5.0 4.9 5.1 5.2 5.0 5.1 5.2 4.8
4.3 4.7 4.8 4.9 4.6 4.8 4.9 4.5


i want to print those two lines for every pair of skaters!(there's 10 pairs) but its not working! :confused:

PLEASE HELP!!!!! Text file is attached! :confused: :( :sorry:
 

Attachments

  • Pairs.txt
    900 bytes · Views: 96

amilabanuka

Well-known member
  • Sep 30, 2006
    7,291
    878
    113
    Thama math hoyanooo....
    Issue 1:
    * you just print the array object in the main method. that will contain the the results of last player.

    solution: add getter and print or add a method to skater to return the correct string. second solution is the best one but for the simplicity I have added 1st solution.

    Issue 2:
    *In Line 17 and 18, you initialize the array.
    *Once the array is filled with individual's scores you create the skater instance passing the array references. but for the next skater, you reuse the same array. the content of the array is overridden.
    * so at the end of reading all the skater objects will have same two arrays and it will contain the results of last read
    * you'll get the results of the last read.


    a solution
    initialize the array for each iteration.


    Code:
    import java.io.*;
    import java.util.Scanner;
    public class testSkaters {
    
        private static final int NUMBER_OF_JUDGES = 8;
    
        public static void main(String[] args) throws IOException {
    
    
    
            Skaters [] arrSkaters = new Skaters [50];
    
            File myFile = new File ("Pairs.txt");
    
            Scanner input = new Scanner (myFile);
    
            int countSkaters = 0;
            String name1;
            String name2;
            String country;
            double [] arr1 = null;
            double [] arr2 = null;
    
    
            while (input.hasNext())
    
            {
    
                name1 = input.nextLine();
                name2 = input.nextLine();
                country = input.nextLine();
                arr1=new double[8];
                for (int i = 0; i < NUMBER_OF_JUDGES; i++)
    
                    arr1[i] = input.nextDouble();
    
                arr2=new double[8];
                for (int j =0; j < NUMBER_OF_JUDGES; j++)
    
                    arr2[j] = input.nextDouble();
    
    
                Skaters s1 = new Skaters (name1, name2, country, arr1, arr2);
    
                arrSkaters[countSkaters] = s1;
    
                countSkaters++;
    
                input.nextLine();
    
            }
    
    
    
            for (int t = 0; t < countSkaters; t++)
    
            {
                System.out.println("Name 1: " + arrSkaters[t].getName1());
    
                System.out.println("Name 2: " + arrSkaters[t].getName2());
    
                System.out.println("Country: " + arrSkaters[t].getCountry());
                printArray(arrSkaters[t].getArrTech());
    
            }
    
    
        }
    
    
        public static void printArray (double [] arr3)
        {
            for (int r = 0; r < arr3.length;r++)
    
                System.out.print(arr3[r]);
            System.out.println("");
    
    
        }
    
    }
     

    Tulip_Doll

    Member
    Feb 6, 2015
    208
    13
    0
    Florida
    Issue 1:
    * you just print the array object in the main method. that will contain the the results of last player.

    solution: add getter and print or add a method to skater to return the correct string. second solution is the best one but for the simplicity I have added 1st solution.

    Issue 2:
    *In Line 17 and 18, you initialize the array.
    *Once the array is filled with individual's scores you create the skater instance passing the array references. but for the next skater, you reuse the same array. the content of the array is overridden.
    * so at the end of reading all the skater objects will have same two arrays and it will contain the results of last read
    * you'll get the results of the last read.


    a solution
    initialize the array for each iteration.


    Code:
    import java.io.*;
    import java.util.Scanner;
    public class testSkaters {
    
        private static final int NUMBER_OF_JUDGES = 8;
    
        public static void main(String[] args) throws IOException {
    
    
    
            Skaters [] arrSkaters = new Skaters [50];
    
            File myFile = new File ("Pairs.txt");
    
            Scanner input = new Scanner (myFile);
    
            int countSkaters = 0;
            String name1;
            String name2;
            String country;
            double [] arr1 = null;
            double [] arr2 = null;
    
    
            while (input.hasNext())
    
            {
    
                name1 = input.nextLine();
                name2 = input.nextLine();
                country = input.nextLine();
                arr1=new double[8];
                for (int i = 0; i < NUMBER_OF_JUDGES; i++)
    
                    arr1[i] = input.nextDouble();
    
                arr2=new double[8];
                for (int j =0; j < NUMBER_OF_JUDGES; j++)
    
                    arr2[j] = input.nextDouble();
    
    
                Skaters s1 = new Skaters (name1, name2, country, arr1, arr2);
    
                arrSkaters[countSkaters] = s1;
    
                countSkaters++;
    
                input.nextLine();
    
            }
    
    
    
            for (int t = 0; t < countSkaters; t++)
    
            {
                System.out.println("Name 1: " + arrSkaters[t].getName1());
    
                System.out.println("Name 2: " + arrSkaters[t].getName2());
    
                System.out.println("Country: " + arrSkaters[t].getCountry());
                printArray(arrSkaters[t].getArrTech());
    
            }
    
    
        }
    
    
        public static void printArray (double [] arr3)
        {
            for (int r = 0; r < arr3.length;r++)
    
                System.out.print(arr3[r]);
            System.out.println("");
    
    
        }
    
    }

    this helped thanks a lot!!! but the second line wont work now :(