Reputation: 1
I created this code to average test scores, however I put each students test1, test2, test3, etc in an array. Now how can I get the overall average of the test score for one test (ex. q1 is in array index 0, but they are 8 different q1's. How do I get their average?)
import java.io.File;
import java.io.IOException;
import java.util.*;
public class Project3 {
public static void main(String[] args) throws IOException {
ReadFile r = new ReadFile();
r.openFile();
r.readFile();
r.closeFile();
--
import java.io.*;
import java.lang.*;
import java.util.*;
public class Grades {
private Formatter x;
//Formatter variable- output string to file
public void openFile(){
//method to open file
try{
x = new Formatter("grades.txt");
}
catch(Exception e){
System.out.println("Could not find file.");
}
}
public void addRecords(){
x.format("%d",8);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", " Thui", "Bhu", 100, 90, 80, 100, 89, 99, 88);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n ArianaB", "Smith", 90, 90, 100, 100, 99, 100, 95);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Emily", "Gonzales", 100, 90, 100, 70, 78, 78, 80);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Jenifer", "L", 80, 90, 90, 100, 89, 99, 85);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Maria", "Jones", 65, 72, 77, 68, 62, 70, 65);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Bill", "Gates", 60, 54, 38, 62, 65, 60, 50);
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Escobar", "Morris", 83, 77, 88, 76, 79, 72, 76 );
x.format(" %s %s, %d, %d, %d, %d, %d, %d, %d", "\n Anne", "Latner", 80, 90, 85, 95, 90, 95, 90 );
}
public void closeFile(){
x.close();
}
//
}
--
import java.io.*;
import java.util.InputMismatchException;
import java.util.*;
public class ReadFile {
private Scanner x;
public void openFile(){
try{
x = new Scanner (new File ("grades.txt"));
}
catch(Exception e){
System.out.println("could not find file");
}}
public void createFile()throws IOException {
try{
File file = new File("grades.txt");
Scanner s = new Scanner(file);
while(s.hasNext()){
{
double q1average = 0.0, q2average = 0.0, q3average = 0.0, q4average = 0.0;
double proji = 0.0, projii =0.0, projiii = 0.0;
double q1sum = 0.0, q2sum = 0.0, q3sum = 0.0, q4sum = 0.0;
double proji_sum = 0.0, projii_sum = 0.0, projiii_sum =0.0;
while(s.hasNext()) {
String[] split = s.nextLine().split(", ");
q1sum += Double.parseDouble(split[1]); // add all first grades
q1average++;
q2sum += Double.parseDouble(split[2]); // add all first grades
q2average++;
q3sum += Double.parseDouble(split[3]); // add all first grades
q3average++;
q4sum += Double.parseDouble(split[4]); // add all first grades
q4average++;
proji_sum += Double.parseDouble(split[5]); // add all first grades
proji++;
projii_sum += Double.parseDouble(split[6]); // add all first grades
projii++;
projiii_sum += Double.parseDouble(split[7]); // add all first grades
projiii++;
int maxq1 =0;
if(Double.parseDouble(split[1]) > maxq1)
maxq1 = (int) Double.parseDouble(split[1]);
System.out.println(maxq1);
}
double q1_average = (q1sum/q1average);
double q2_average = (q2sum / q2average);
double q3_average = (q3sum / q3average);
double q4_average = (q4sum / q4average);
double p1_average = (proji_sum/proji);
double p2_average = (projii_sum/projii);
double p3_average = (projiii_sum/projiii);
}
}}catch(Exception e){
e.printStackTrace();
}
}
public void closeFile(){
x.close();
}
}
Upvotes: 0
Views: 128
Reputation: 37845
Basically what you have is a multidimensional array. You have multiple test scores ("rows") on multiple lines ("columns"). You need to put each line somewhere so you can recall them later.
You can either do something simple like this:
double grades = 0.0;
double sum = 0.0;
while(s.hasNext()) {
String[] split = s.nextLine().split(", ");
sum += Double.parseDouble(split[1]); // add all first grades
grades++;
}
System.out.println(sum / grades);
Or something more complex:
List<ArrayList<String>> allGrades = new ArrayList<ArrayList<String>>();
while(s.hasNext()) {
allGrades.add(Arrays.asList(s.nextLine().split(", ")));
}
StringBuilder results = new StringBuilder();
int cols = allGrades.get(0).size();
for(int c = 1; ;) { // outer for every test
double sum = 0.0;
double count = 0.0;
double max = 0.0;
for(int r = 0; r < allGrades.size(); r++) { // inner for every person
count++;
double grade = Double.parseDouble(allGrades.get(r).get(c));
sum += grade;
if(grade > max) {
max = grade;
}
}
results.append("[test ");
results.append(c);
results.append(": avg = ");
results.append(sum / count);
results.append(", max = ");
results.append(max);
results.append(']');
if(++c < cols) { // fence post logic...
results.append(", ");
} else {
break;
}
}
System.out.println(results);
Or maybe like this:
double[] avgs = new double[7];
double count = 0.0;
while(s.hasNext()) {
String[] split = s.nextLine().split(", ");
for(int i = 0, k = 1; k < split.length; i++, k++) {
avgs[i] += Double.parseDouble(split[k]);
}
count++;
}
for(int i = 0; i < avgs.length; i++) {
System.out.println("test avg " + (i + 1) + ": " + (avgs[i] / count));
}
Upvotes: 0
Reputation: 371
This will be not very nice way to make it, but it should work. So, your array is split[]
and it stores all data splitted by ,
(comma). So if you notice,if you add +8 index
to your first Student q
's (q1
for example is stored in split[1]
, so +8, and second Student
q1
will be stored in split[9]
) you will get your second Student q's. So can do something like this:
int std=0
int q1=0,a=1;
while(a<split.length){
q1+=Integer.parseInt(split[a]);
a+=8;
std++;
}
int result = q1/std;
// This will count all students q1 and take average
The same you can make with others q's. But as I said, this is boilerplate code.
Upvotes: 0
Reputation: 8825
The same way you would get the average on paper: add all the individual test scores and divide by their total number. So create a temporary variable and then, in a loop, add up the individual test scores, dividing by the total number of students after the loop.
Upvotes: 2