Can you help me write this program using Java? This is for Introduction to Compu
ID: 3862975 • Letter: C
Question
Can you help me write this program using Java? This is for Introduction to Computer Science class and we use Java. The problem I'm having is when I create the array to store the data, I dont know how to isolate the integers to find the scores average. Please keep in mine this is an introductory level class.
This is file "grades.csv". It contains a list of students and their exam grades.:
Arthur Albert,74%
Melissa Hay,72%
William Jones,85%
Rachel Lee,68%
Joshua Planner,75%
Jennifer Ranger,76%
Richard Rock,80%
Jerry Rose,80%
Dorothy Smith,76%
Jon Smith,60%
Lisa Smith,90%
Michael Stone,13%
Josephine Trenton,75%
Franklin Williams,58%
Write a program in Java that reads in the file and computes the average grade for the class and then prints it to the console.
Explanation / Answer
ReadGrades.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DecimalFormat;
public class ReadGrades {
public static void main(String[] args) {
File csvFileName = new File("grades.csv");
String line = "";
int count = 0;
double totalGrade = 0;
try {
BufferedReader br = new BufferedReader(new FileReader(csvFileName)) ;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
double grade = Double.parseDouble(values[1].substring(0, values[1].length()-1));
totalGrade = totalGrade + grade;
count++;
}
double averageGrade = totalGrade/count;
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("Average grade is "+df.format(averageGrade));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output:
Average grade is 70.14
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.