Question
JAVA: Why isnt this running import java.io.*; import java.util.*; import java.lang.*; import java.io.FileNotFoundException; public class StudentReport { // TODO Auto-generated method stub public static String[]
JAVA: Why isnt this running
import java.io.*; import java.util.*; import java.lang.*; import java.io.FileNotFoundException;
public class StudentReport {
// TODO Auto-generated method stub
public static String[] students ; public static int [][]grades ; public static void main(String[] args) throws IOException { readFile(); //System.out.println(students[0] + " " + grades[0]); Scanner scan = new Scanner(System.in);
System.out.println("Enter '1' for a list of students: "); System.out.println("Enter '2' for a report card for a student"); System.out.println("Enter '3' for stats"); int choice = scan.nextInt(); scan.nextLine(); switch (choice) { case 1: for (int i = 0 ; i < students.length ; i++) { System.out.println(students[i]); } break; case 2: System.out.println("Enter the name of the student"); String name = scan.nextLine(); writeFile(name); break; case 3: findStat(); System.out.println("Average: " + average); System.out.println("Highest Total: " + high); System.out.println("Lowest Total: " + low); break; } }
public static void readFile() throws IOException {
//File file = new File("Grades.csv"); String info[] ; int ind = 0 ; try { Scanner sc = new Scanner(new File("Grades.csv")); String random = sc.nextLine() ; while (sc.hasNext()) { info = sc.nextLine().split(","); students[ind] = info[0]; grades[ind][0] = Integer.parseInt(info[1]) ; grades[ind][1] = Integer.parseInt(info[2]) ; grades[ind][2] = Integer.parseInt(info[3]) ; ind++; } sc.close(); } catch (FileNotFoundException e) { System.out.println("** File Not Found **");
} }
private static void writeFile(String name) throws IOException { double findAvg = 0.0 ; int nameIndex = 0 ; for (int i = 0; i < students.length ; i++) { if (students[i].equalsIgnoreCase(name)) { nameIndex = i ; } } for (int i = 0 ; i < grades.length ; i++) { findAvg+= grades[nameIndex][i] ; } findAvg = findAvg/3 ; System.out.println("Student: "+ students[nameIndex] ); System.out.println("Quiz 1: "+ grades[nameIndex][0] ); System.out.println("Quiz 2: "+ grades[nameIndex][1] ); System.out.println("Quiz 3: "+ grades[nameIndex][2] ); //try (FileWriter writer = new FileWriter(filename)) try(PrintWriter writer = new PrintWriter("reportCard.txt")) { writer.write("Student: "+ students[nameIndex] + " "); writer.write("Quiz 1: " + grades[nameIndex][0] + " "); writer.write("Quiz 2: " + grades[nameIndex][1] + " "); writer.write("Quiz 2: " + grades[nameIndex][2] + " "); writer.write("Letter Grade: " + getGrade(findAvg) + " "); writer.close(); } catch (IOException e) { System.out.println("file execption"); } }
private static String getGrade(double v) { if (v >= 9) { return "A"; } else if (v >= 8) { return "B"; } else if (v >= 7) { return "C"; } else if (v >= 6) { return "D"; } return "F"; }
private static double high; private static double low; private static double average;
private static void findStat() { high = 0.0; low = 0.0; double sum = 0; for (int i = 0 ; i < students.length ; i++) { for (int j =0 ; i < 3 ; i++ ) { if(grades[i][j] > high){ high = grades[i][j]; } if(grades[i][j]< low) { low = grades[i][j] ; } sum+=grades[i][j]; } } average = sum / (grades.length*3); }
}
SAMPLE .csv
name,quiz1,quiz2,quiz3
mike,9,8,10
matt,7,5,10
sarah,10,4,2
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