I have a program so far that outputs the number of As and Fs there were for a specific academic year. I would like to know how to output the academic year out of all the academic years that had the most As based on a certain file.
Here is the code I have thus far:
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class GradesProceduralProgram { //reading data into arrays public static String[] academicYear = new String[6446]; public static String[] term = new String[6446]; public static String[] course = new String[6446]; public static char[] grade = new char[6446]; public static void main(String[] args) { File file = new File("gradesCPSC.csv"); Scanner input = null; try { input = new Scanner(file); } catch (FileNotFoundException e) { System.out.println("File not found."); e.printStackTrace(); System.exit(0); }//end catch //read header to text file and do nothing with it input.nextLine(); int index = 0; while(input.hasNext()) { String line = input.nextLine(); String[] fields = line.split(","); academicYear[index] = fields[0]; term[index] = fields[1]; course[index] = fields[2]; grade[index++] = fields[3].charAt(0); }//end while //close file as we are done using it after all the data is loaded into our arrays in the while loop input.close(); int numOfStudentsA = countOfPassingStudents("2018-19", "CPSC-39", 'A'); System.out.println("How many students from CPSC-39 received a grade of 'A' in the 2018-19 school year?\n" + numOfStudentsA); int numOfStudentsF = countOfFailingStudents("2018-19", "CPSC-39", 'F'); System.out.println("\nHow many students from CPSC-39 received a grade of 'F' in the 2018-19 school year?\n" + numOfStudentsF); /*int yearWithMostAs = numOfAs('A'); System.out.println("\nWhich year did students receive the most 'A's for CPSC-39?\n" + yearWithMostAs);*/ }//end main public static int countOfPassingStudents(String year, String className, char gradeA) { int countA = 0; for(int i = 0; i < academicYear.length; i++) { if(year.equalsIgnoreCase(academicYear[i]) && className.equalsIgnoreCase(course[i]) && gradeA == grade[i]) { countA++; }//end if }//end for return countA; }//end students with A method public static int countOfFailingStudents(String year, String className, char gradeF) { int countF = 0; for(int i = 0; i < academicYear.length; i++) { if(year.equalsIgnoreCase(academicYear[i]) && className.equalsIgnoreCase(course[i]) && gradeF == grade[i]) { countF++; }//end if }//end for return countF; }//end students with F method public static int numOfAs(char gradeAs) { int countNum = 0; for(int i = 0; i < academicYear.length; i++) { if(gradeAs == grade[i]) { countNum++; }//end if }//end for return countNum; }//end year students received the most As method }//end class
Here is the output:
How many students from CPSC-39 received a grade of 'A' in the 2018-19 school year? 14 How many students from CPSC-39 received a grade of 'F' in the 2018-19 school year? 18
Some data from the file I am using(I can't post all of the data or it'll crash this site for me :p) :
AY,Term,Course,Grade 2014-15,2014U,CPSC-30,A 2014-15,2014U,CPSC-30,A 2015-16,2015U,CPSC-30,A 2015-16,2015U,CPSC-30,A 2016-17,2016U,CPSC-30,A 2016-17,2016U,CPSC-30,A 2017-18,2017U,CPSC-30,A 2017-18,2017U,CPSC-30,A 2018-19,2018F,CPSC-01,A 2018-19,2018F,CPSC-01,A 2019-20,2019F,CPSC-39,A 2019-20,2019F,CPSC-39,A