Причина, по которой ваш код многократно печатает средние оценки, заключается в том, что этот метод здесь
public static double studentAverage(int testQty) throws IOException
{
File infile = new File("Grades.txt");
Scanner scanavg = new Scanner(infile);
double studentSum = 0;
scanavg.nextLine();
scanavg.next();
for(int i = 1; i <= testQty; i++) {
studentSum += scanavg.nextDouble();
}
double studentAvg = studentSum / testQty;
return studentAvg;
}
Проблема в том, что, хотя вы думаете, что читаете последовательные строки, вы на самом деле просто читаете первую строкуфайла всякий раз, когда выполняются эти методы, и, следовательно, вы получаете первые оценки учащихся.
A Решение
Если вам удобно Streams и java.nio, вы можете сделать это немного более элегантно и функционально, используя Files.lines ()
public static void main(String... args) throws IOException {
// The following line reads each line from your data file and does some
// with it.
Files.lines(Paths.get("/path/to/your/file")).forEach(ThisClass::doSomethingWithLine);
System.out.println("clas average is "+ (classMarks / totalStuds));
}
static double classMarks=0; //total marks of the class
static int totalStuds=0; // total students read until now
static void doSomethingWithLine(String line) {
Scanner sc = new Scanner(line);
// Parse the tokens
// student name (assuming only first name according to your
// example) otherwise use pattern to parse multi part names
String name = sc.next();
// parse student marks
// (you could create a method for this to make things cleaner)
int subA = sc.nextInt();
int subB = sc.nextInt();
int subC = sc.nextInt();
//calculate average
double avg = (subA + subB + subC) / 3;
// calculate class average
classMarks = classMarks + avg
totalStuds++;
//determine grade
char grade = determineGrade(avg);
System.out.println(name + " " + avg + " " + grade);
}