Question
I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5
I have this program that works but not for the correct input file. I need the program to detect the commas
Input looks like:
first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95
output needs to look like:
Tom Brady
--------------------------
Assignment 1: A
Assignment 2: A
Assignment 3: E
Assignment 4: A
Assignment 5: C
Final Grade: 82.4 = B
The current program:
import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner;
public class main {
public static void main(String[] args) throws IOException { Scanner sc = null; String fname, lname; int score; String namaes[] = null; double tot = 0, finalGrade = 0; double grades[] = null; int cnt = 0; try {
sc = new Scanner(new File("students.txt")); sc.nextLine(); while (sc.hasNext()) { sc.nextLine(); cnt++; } sc.close();
namaes = new String[cnt]; grades = new double[cnt]; sc = new Scanner(new File("students.txt")); sc.nextLine(); for (int i = 0; i < cnt; i++) { tot = 0; fname = sc.next(); lname = sc.next(); namaes[i] = fname + " " + lname; for (int j = 0; j < 5; j++) { tot += sc.nextInt(); } finalGrade = tot / 5; grades[i] = finalGrade; } sc.close(); double average = avg(grades); double minimum = min(grades); double maximum = max(grades);
System.out.println("Average of all Final Grades :" + average); System.out.println("Minimum of all Final Grades :" + minimum); System.out.println("Maximum of all Final Grades :" + maximum);
FileWriter fw = new FileWriter(new File("OutputFile.txt")); sc = new Scanner(new File("students.txt")); sc.nextLine(); for (int i = 0; i < cnt; i++) { fw.write(sc.next() + " " + sc.next() + ":"); for (int j = 0; j < 5; j++) { fw.write(findgradeLetter(sc.nextInt()) + " "); } fw.write(grades[i] + " "); }
sc.close(); fw.close();
} catch (FileNotFoundException e) { System.out.println(e); System.exit(0); }
}
private static char findgradeLetter(double average) { char gradeLetter = 0; if (average >= 90 && gradeLetter <= 100) gradeLetter = 'A'; else if (average >= 80 && average < 90) gradeLetter = 'B'; else if (average >= 70 && average < 80) gradeLetter = 'C'; else if (average >= 60 && average < 70) gradeLetter = 'D'; else if (average < 60) gradeLetter = 'F';
return gradeLetter; }
private static double max(double[] grades) { double max = grades[0]; for (int i = 0; i < grades.length; i++) { if (max < grades[i]) { max = grades[i]; } } return max; }
private static double min(double[] grades) { double min = grades[0]; for (int i = 0; i < grades.length; i++) { if (min > grades[i]) { min = grades[i]; } } return min; }
private static double avg(double[] grades) { double sum = 0; for (int i = 0; i < grades.length; i++) { sum += grades[i]; } return sum / grades.length; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started