/home/wpollock1/public_html/Java/DeansList.java
// DeansList.java - A program to read in a bunch of student records
// from a file, and print all student names who have a GPA greater than
// 3.0 (the cut-off for making the Dean's list). The file format is:
// count_of_records (int) <newline>
// name (String) <tab> GPA (double) <newline>
//
// The file name is passed as a command line argument to the program.
//
// Storing the data in a collection is NOT required in this case,
// since the data only needs to be processed once. Compare this with the
// program AverageGPA.java.
//
// Written 2018 Wayne Pollock, Tampa FL USA.
import java.nio.file.Paths;
import java.util.*;
public class DeansList {
final static double DEANS_LIST_MINIMUM__GPA = 3.00;
public static void main ( String [] args ) {
Scanner in;
// Filename should be a file in (or relative to) the current directory:
if ( args.length != 1 ) {
System.out.println( "\nUsage: java DeansList <name_of_datafile>"
+ "\n(example: java DeansList StudentRecords.txt)" );
return;
}
try { // Make a Scanner from the filename:
in = new Scanner( Paths.get(args[0]) );
in.nextLine(); // Throw away the count of records line.
// Use tabs and newlines as field delimiters for rest of file:
in.useDelimiter( "[\\t\\r\\n]" );
}
catch ( Exception e ) {
System.err.println( "\n*** Could not read number of records from file"
+ " (the first line).\n" + e.toString() );
return;
}
// Display a title for the output list:
System.out.printf(
"%n Students on Dean's List (GPA greater than %.2f):%n%n",
DEANS_LIST_MINIMUM__GPA );
boolean atLeastOne = false;
while ( in.hasNextLine() ) {
try {
String name = in.next(); // Read up to a tab
double GPA = in.nextDouble();
in.nextLine(); // Skip over the newline
if ( GPA > DEANS_LIST_MINIMUM__GPA ) {
System.out.printf( "\t%-18s (%.2f)%n", name, GPA );
atLeastOne = true;
}
}
catch ( InputMismatchException ime ) {
System.err.println( "Error: Badly formatted input data" );
return; // TODO probably should continue, to just skip bad data.
}
}
if ( ! atLeastOne ) {
System.out.println(" (No students made the Dean's list this time.)");
}
}
}