AverageGPA.java
Download AverageGPA.java
1: // AverageGPA.java - A program to read in a bunch of student records
2: // from a file, and print all student names who have a greater than
3: // average GPA. The file format is:
4: // count_of_records (int) <newline>
5: // name (String) <tab> GPA (double) <newline>
6: //
7: // An ArrayList might be a better choice for this particular
8: // application, but I wanted to show array processing so I added a count
9: // of the records as the first line of the file. The file name is
10: // passed as a command line argument to the program.
11: //
12: // Storing the data in a collection (array in this case) is required,
13: // since the data must be processed twice: once to compute the average; then
14: // after that, to print those with GPAs above average. Without a collection,
15: // the data would need to be read in a second time.
16: //
17: // Written 2018 Wayne Pollock, Tampa FL USA.
18:
19: import java.nio.file.Paths;
20: import java.util.*;
21:
22: public class AverageGPA {
23: public static void main ( String [] args ) {
24: Scanner in;
25: int numRecords = 0;
26: double runningTotal = 0.0;
27:
28: // Filename should be a file in (or relative to) the current directory:
29: if ( args.length != 1 ) {
30: System.out.println( "\nUsage: java AverageGPA <name_of_datafile>"
31: + "\n(example: java AverageGPA StudentRecords.txt)" );
32: return;
33: }
34:
35: try { // Make a Scanner from the filename:
36: in = new Scanner( Paths.get(args[0]) );
37: numRecords = in.nextInt();
38: in.nextLine(); // Throw away the newline
39: // Use tabs and newlines as field delimiters for rest of file:
40: in.useDelimiter( "[\\t\\r\\n]" );
41: }
42: catch ( Exception e ) {
43: System.err.println( "\n*** Could not read number of records from file"
44: + " (the first line).\n" + e.toString() );
45: return;
46: }
47:
48: if ( numRecords < 1 ) {
49: System.err.println(
50: "\n*** Number of records in file is less than 1!\n" );
51: return;
52: }
53:
54: Student[] student = new Student[numRecords];
55:
56: for ( int i = 0; i < numRecords; ++i ) {
57: try {
58: String name = in.next(); // Read up to a tab
59: double GPA = in.nextDouble();
60: in.nextLine(); // Skip over the newline
61:
62: student[i] = new Student( name, GPA );
63: runningTotal += GPA;
64: }
65: catch ( InputMismatchException ime ) {
66: System.err.println( "Error: Badly formatted input data" );
67: return; // TODO probably should continue, to just skip bad data.
68: }
69: }
70:
71: // Compute the average:
72: double average = runningTotal / numRecords;
73:
74: // Display a title for the output table:
75: System.out.printf( "\n GPA greater than %4.2f:\n", average );
76:
77: for ( final Student s : student ) {
78: if ( s.getGPA() > average ) {
79: System.out.println( "\t" + s.toString() );
80: }
81: }
82: // TODO: check for zero students > average, and display message if none.
83: // (That can only happen if all the students have the same GPA.)
84: }
85: } // End of class AverageGPA
86:
87: // This is a trivial class Student, containing only a name and GPA:
88: class Student {
89: private final String name;
90: private final double GPA;
91:
92: public Student ( String name, double GPA ) {
93: this.name = name;
94: this.GPA = GPA;
95: }
96:
97: public String getName () {
98: return name;
99: }
100:
101: public double getGPA () {
102: return GPA;
103: }
104:
105: // Format a student record nicely:
106: public String toString () {
107: return "Name: " + name + "\tGPA: " + String.format( "%4.2f", GPA );
108: }
109: }