So the problem comes in, in the last method called printComparisonResults. I just have no clue what to do?? Thanks for your time!


My Class:

//StudentRecord
 //Nick Elliott 02/01/10
 
 import java.util.Scanner;
 import java.io.*;


 
 // A StudentRecord contains a student's first name and their gpa
 public class StudentRecord
 {
 	private String name;
	private double gpa;
	
	//initialize record given to values
	public StudentRecord(String theName, double theGpa)
	{
		this.setName(theName);
		this.setGpa(theGpa);
	}
	// initialize the empty record
	public StudentRecord( )
	{
		this("",0);
	}
	// initialize record to values given in string
	public StudentRecord(String recordValues)
	{
		Scanner scanner = new Scanner(recordValues);
		this.setName(scanner.next());
		this.setGpa(scanner.nextDouble());
	}
	// name is set to theName
	public void setName(String theName)
	{
		this.name = theName;
	}
	// gpa is set to theGpa
	public void setGpa(double theGpa)
	{
		if(theGpa < 0.0 || theGpa > 4.0)
		{
			throw new RuntimeException(theGpa + " is out of the range, 0.0 - 4.0");
		}
		
		this.gpa = theGpa;
	}
	// returns name
	public String getName( )
	{
		return this.name;
	}
	// returns gpa
	public double getGpa( )
	{
		return this.gpa;
	}
	//returns string representation of record
	public String toString( )
	{
		return this.name + " " + this.gpa;
	}
}

Here is my application:

// FindAverageGpa
// Nick Elliott 2/2/10

import java.io.*;
import java.util.Scanner;

public class FindAverageGpa
{
	// read in a list of StudentRecords
	// print out the average gpa and whether each student's gpa is >= or <average
	public static void main(String[]args)throws IOException
	{
		final int MAX_SIZE = 10;
		StudentRecord[] student = new StudentRecord[ MAX_SIZE ];
		
		System.out.println(" Type in a list of students names and their Gpa's"
		+ " \nPress enter at blan line to stop");

		int size = readRecords( student );
		
		double averageGpa = calculateAverageGpa( student, size);
		
		System.out.println(" the average gpa is " + averageGpa);

		printComparisonResults( student, size, averageGpa);

}
	// interactively read in any number of StudentRecords (name gpa) into the student array
	// return the number of records read and stored
	public static int readRecords( StudentRecord[]student) throws IOException
	{
		Scanner scanner = new Scanner(System.in);

		String line = null;
		int count = 0;
		while(true)
		{
			line = scanner.nextLine();
			
			if(line.equals(""))
				break;
			
			data[count] = new StudentRecord (line);
			
			count++;
		}
		return count; // number of data elements stored
	}

	// returns the average gpa of the records stored in student[0...size-]
	public static double calculateAverageGpa(StudentRecord[]student, int size)
	{
		double sum = 0.0;
		int count = 0;

		while(count < size)
		{
			sum = sum + student[count].getGpa();
			count++;

		}
		double averageGpa = sum/size;
		return averageGpa;
	}
	
	// print out each StudentRecord and say whether that Student's gpa is >= or < averageGpa
	public static void printComparisonReults(StudentRecord[]student, int size, double averageGpa)
	{
		if(StudentRecord[theGpa] >= averageGpa) //this line has a problem
		{
			System.out.println(" The student record is greater than or equal to the average gpa.");
		}
		else
		{
			if(StudentRecord[theGpa] < averageGpa)//this line has a problem
			{
				System.out.println(" The student record is less than the average gpa.");
			}
		}
	}
}

Dani AI

Generated

Several small but critical issues prevent compilation and correct results. The call from main is to printComparisonResults(...) while the defined method is misspelled printComparisonReults — the names must match. Inside the comparison method the code tries to compare a non-existent identifier and the class name (StudentRecord[theGpa]) to a double; that should be replaced by iterating the student array and calling getGpa() on each element. The input routine writes to an undefined array (data[count]) and has no bounds check, and calculateAverageGpa should guard against size == 0 to avoid division by zero. is correct that a loop is needed; the snippet below implements the loop and adds basic safety checks.

public static int readRecords(StudentRecord[] student) {
    Scanner in = new Scanner(System.in);
    int count = 0;
    while (count < student.length) {
        String line = in.nextLine();
        if (line.trim().isEmpty()) break;
        student[count++] = new StudentRecord(line);
    }
    return count;
}

public static double calculateAverageGpa(StudentRecord[] student, int size) {
    if (size == 0) return 0.0;
    double sum = 0.0;
    for (int i = 0; i < size; i++) sum += student[i].getGpa();
    return sum / size;
}

public static void printComparisonResults(StudentRecord[] student, int size, double averageGpa) {
    for (int i = 0; i < size; i++) {
        StudentRecord s = student[i];
        double g = s.getGpa();
        System.out.printf("%s: %.2f -> %s average%n",
            s.getName(), g, (g >= averageGpa) ? ">= " : "< ");
    }
}

Notes and pitfalls: if input names may contain spaces, the StudentRecord(String) constructor that uses Scanner.next() will only take the first token; either change the parsing or require a single-word name. Always check array bounds when reading, and format the average (and comparisons) for readability. These fixes address the compilation errors, runtime exceptions, and the logical comparison that highlighted.

// print out each StudentRecord and say whether that Student's gpa is >= or < averageGpa
	public static void printComparisonReults(StudentRecord[]student, int size, double averageGpa)
	{
		if(StudentRecord[theGpa] >= averageGpa) //this line has a problem
		{
			System.out.println(" The student record is greater than or equal to the average gpa.");
		}
		else
		{
			if(StudentRecord[theGpa] < averageGpa)//this line has a problem
			{
				System.out.println(" The student record is less than the average gpa.");
			}
		}
	}

First, the variable theGpa does not exist within this method. Second, from the comments, it appears that you want to iterate through each of the student records. Try wrapping a for loop around your if statements and use the loop variable in lieu of theGpa. Lastly, StudentRecord is not an array of doubles, it's not even an array. It's a type. Your array is student of type StudentRecord.

Keep at it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.