CODE
//StudentRecord.java
//Andrew Z Sept 11 2008
import java.util.StringTokenizer;
//A StudentRecord contains a student's first name and their gpa
public class StudentRecord
{
private String name;
private double gpa;
//initialize to the given values
public StudentRecord(String newName, double newGpa)
{
setName(newName);
setGpa(newGpa);
}
//initialize to the empty record
public StudentRecord()
{
this("" , 0);
}
//initialize record to values in given string
public StudentRecord(String studentInfo)
{
StringTokenizer theTokenizer = new StringTokenizer(studentInfo);
setName(theTokenizer.nextToken());
setGpa(Double.parseDouble(theTokenizer.nextToken()));
}
//name is set to newName
public void setName(String newName)
{
name= newName;
}
//gpa is set to newGpa
public void setGpa(double newGpa)
{
gpa= newGpa;
}
//returns name
public String getName()
{
return name;
}
//returns gpa
public double getGpa()
{
return gpa;
}
//returns string representation of record
public String toString()
{
return name+" "+ gpa;
}
}
CODE
//AverageGpa
//Andrew Z Sept 11 2008
import java.io.*;
public class AverageGpa
{
public static void main(String []args) throws IOException
{
StudentRecord[] student = new StudentRecord[10];
System.out.println("Type in a list of a students name seperated by a space, then their gpa, one per line."
+ "\nPress enter at blank line to stop" );
// int size = readDataIntoArray(data);
// }
// public static int readDataIntoArray(Integer[]data) throws IOException
// {
InputStreamReader reader= new InputStreamReader(System.in);
BufferedReader keyReader = new BufferedReader(reader);
String line=null;
int count = 0;
double average=0;
while(true)
{
{
line= keyReader.readLine();
if(line.equals(""))
break;
student[count] = new StudentRecord(line);
count++;
}
}
int track = 0;
for(int x =0; x < count; x++)
{
average = average + student[x].getGpa();
average = average/count;
track++;
}
System.out.println("The average Gpa is " + average);
for(int y=0; y< track; y++)
{
if (student[y].getGpa() > average)
System.out.println(student[y].getName()+ "'s gpa = "+ student[y].getGpa()+ " and is greater than average");
else
System.out.println(student[y].getName()+ "'s gpa= "+ student[y].getGpa()+ " and is less than the average");
}
}
}
The problem I'm having is mymy for loop where i try to get the average I keep getting the wrong average for example when i enter 4 and 2 as my gpa's the average comes out as 2 ?? and the first set of code is my constructor