Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How do I output the academic year with the most As?

  1. #1
    Junior Member
    Join Date
    Aug 2020
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post How do I output the academic year with the most As?

    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
    Last edited by HyperRei; August 27th, 2020 at 11:43 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,426
    Thanks
    53
    Thanked 2,491 Times in 2,442 Posts

    Default Re: How do I output the academic year with the most As?

    I would like to know how to output the academic year out of all the academic years that had the most As
    How and where does the code sum the number of As for each year? Once all the As have been counted, then search the list for the largest value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2020
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I output the academic year with the most As?

    In this part of the code is where I am supposed to do the math or "sum" as you say. The "if (gradeAs == grade[i]) {" is in relation to the line "int yearWithMostAs = numOfAs('A');" but in the actual code I have it commented until I get it working. It's no so much math but rather when there is an instance of an 'A' the part of the program below will add 1 to the count for every A encounted. I'm not sure how to do it based off academic year so that it outputs the academic year (ex. 2018-19) that had the most As.
    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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,426
    Thanks
    53
    Thanked 2,491 Times in 2,442 Posts

    Default Re: How do I output the academic year with the most As?

    how to do it based off academic year
    Using arrays like you currently doing, define an array to hold the counts for each year. For example the first slot would hold the data for the year 2000, the next slot for the year 2001, etc. Then when you read the data, add 1 to the slot for the year when that year has an A.
    The trick is to compute the array's index by subtracting the base year (say 2000) from the year with the A. For example if there was an A in year 2015, add a one to the array slot with index 15. Chose the base year based on the range of years in your data.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2020
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I output the academic year with the most As?

    Got it, I thought about doing it that way but I wondered if there was a shorter way of doing it for efficiency sake. You know? Thanks

Similar Threads

  1. ACADEMIC PROJECT HELP NEEDED !!!
    By jony_asj in forum Java Networking
    Replies: 2
    Last Post: May 17th, 2014, 02:04 PM
  2. Small webapp that determine whether a year is a leap year.
    By frankhvam in forum Object Oriented Programming
    Replies: 0
    Last Post: October 26th, 2013, 03:32 PM
  3. Java Comments Quality - Academic Survey - $10
    By mcmillco in forum Paid Java Projects
    Replies: 1
    Last Post: June 9th, 2013, 08:15 PM

Tags for this Thread