AverageGPA.java

Download AverageGPA.java source file

Download/view StudentRecords.txt data file

// AverageGPA.java - A program to read in a bunch of student records
// from a file, and print all student names who have a greater than
// average GPA (i.e., the Dean's list).  The file format is:
//    count_of_records (int) <newline>
//    name (String) <tab> GPA (float) <newline>
//
// An ArrayList would probably be a better choice for this particular
// application, but I wanted to show array processing so I added a count
// of the records as the first line of the file.  The file name is
// passed as a command line argument to the program.  Note the scanner uses
// either \n or \r for a delimiter, meaning this should work for either
// Unix or DOS formatted text.
//
// Written 2004 Wayne Pollock, Tampa FL USA.
// Updated 2015 by WP for Java 7 features.

import java.nio.file.*;
import java.util.*;

public class AverageGPA
{
   public static void main ( String [] args )
   {
      Scanner in;
      int numRecords = 0;
      // An ArrayList would be better, but we haven't learned those yet:
      Student [] student;
      float runningTotal = 0.0f;

      // Filename should be a file in (or relative to) the current directory:
      if ( args.length != 1 )
      {  System.out.println( "\nUsage: java AverageGPA <name_of_datafile>"
            + "\n(example: java AverageGPA StudentRecords.txt)" );
         return;
      }

      try
      {  // Make a Scanner from the pathname of filename:
         in = new Scanner( Paths.get(args[0]) );
         numRecords = in.nextInt();
         in.nextLine();  // Throw away the newline
         // Use tabs and newlines as field delimiters for rest of file:
         in.useDelimiter( "[\\t\\r\\n]" );
      }
      catch ( Exception e )
      {  System.out.println( "\n*** Could not read number of records from file "
           + "(the first line).\n" + e.toString() );
         return;
      }

      student = new Student[numRecords];  // TODO: Validate number as > zero.

      for ( int i = 0; i < numRecords; ++i )
      {  try
         {   String name = in.next();  // Read up to a tab
            float GPA = in.nextFloat();
            in.nextLine();  // Skip over the newline

            student[i] = new Student( name, GPA );
            runningTotal += GPA;
         }
         catch ( InputMismatchException ime )
         {   System.err.println( "Error: Badly formatted input data" );
            return;
         }
      }

      // Compute the average:
      if ( numRecords == 0 )
         return;
      double average = (double) runningTotal / numRecords;

      // Display a title for the output table:
      System.out.printf( "\n       Dean's List (GPA greater than %4.2f):\n",
            average );

      for ( Student s : student )
      {
         if ( s.getGPA() > average )
            System.out.println( "\t" + s.toString() );
      }
      // TODO: check for > zero students on list, and display message if none.
   }

}  // End of class AverageGPA


// This is a trivial class Student, containing only a name and GPA:
class Student
{
   private final String name;
   private final float GPA;

   public Student ( String name, float GPA )
   {  this.name = name;
      this.GPA = GPA;
   }

   public String getName ()
   {  return name;
   }

   public float getGPA ()
   {  return GPA;
   }

   // Format a student record nicely:
   public String toString ()
   {  return "Name: " + name + "\tGPA: " + String.format( "%4.2f", GPA );
   }
}