Advertisement
binibiningtinamoran

ClassAverages.java

Jun 3rd, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.InputMismatchException;
  3. import java.util.Scanner;
  4.  
  5. public class ClassAverages {
  6.     public static void main(String[] args) throws FileNotFoundException {
  7.  
  8.         Scanner scan = new Scanner(System.in);
  9.         System.out.print("\nEnter the filename: ");
  10.         String filename = scan.nextLine();
  11.         filename += ".txt";
  12.  
  13.         System.out.println("\nCalling \"inputScores()\" method...");
  14.         inputScores(filename);
  15.  
  16.         System.out.println("\nCalling \"outputScores()\" method...");
  17.         outputScores(filename);
  18.  
  19.     } // end main()
  20.  
  21.     private static void inputScores(String someFileName) throws FileNotFoundException {
  22.  
  23.         Scanner scan = new Scanner(System.in);
  24.         int studentNumber = 1;
  25.  
  26.             try {
  27.                 PrintWriter outputFile = new PrintWriter(someFileName); // create an instance of the
  28.                 // PrintWriter class to write data to file
  29.  
  30.                 System.out.printf("\nEnter score for user %,d: (enter -1 to exit): ", studentNumber);
  31.                 double score = scan.nextDouble();
  32.                 System.out.println(); // spacing for readability
  33.                 while (score < -1.0) {
  34.                     System.out.printf("A score of %,.2f is invalid.", score);
  35.                     System.out.printf("\nEnter score for user %,d: (enter -1 to exit): ", studentNumber);
  36.                     score = scan.nextInt();
  37.                     System.out.println(); // spacing for readability
  38.                 }
  39.  
  40.                 while (score != -1) {
  41.                     outputFile.println(score); // println method writes a line of data to a file.
  42.                     studentNumber++; // increment Student #
  43.                     System.out.printf("Enter score for user %,d: (enter -1 to exit): ", studentNumber);
  44.                     score = scan.nextDouble();
  45.                     System.out.println(); // spacing for readability
  46.  
  47.                     while (score < -1.0) {
  48.                         System.out.printf("A score of %,.2f is invalid.", score);
  49.                         System.out.printf("\nEnter score for user %,d: (enter -1 to exit): ", studentNumber);
  50.                         score = scan.nextDouble();
  51.                     }
  52.                 }
  53.                 System.out.println("All values entered have been written to the file " +
  54.                         "\"" + someFileName + "\"");
  55.                 outputFile.close();
  56.                 scan.close();
  57.             } catch (InputMismatchException e) {
  58.                 System.out.println("\nInvalid input detected. \nEnding program now.\nGoodbye!");
  59.                 //System.out.println("ERROR: " + e.toString());
  60.                 System.exit(0);
  61.         }
  62.  
  63.     } // end inputScores()
  64.  
  65.     private static void outputScores(String someFileName) throws FileNotFoundException {
  66.         System.out.println("____________________________________________________________");
  67.         System.out.printf("\n\nOPENING FILE \"%s\"\n", someFileName);
  68.         // creates an instance of the File class
  69.         // Pass String parameter to the File object constructor
  70.         File fileToRead = new File(someFileName);
  71.         if (!fileToRead.exists()) { // Not necessary but for safe-handling, just in case
  72.             System.out.println("\nFile does not exist.");
  73.             System.exit(0);
  74.         }
  75.  
  76.         // Pass a reference to this File object as an argument to the Scanner class constructor
  77.         // This creates a Scanner object that uses the file someFileName as its source of input.
  78.         Scanner fileToReadScanner = new Scanner(fileToRead);
  79.         int index = 1;
  80.         double sumOfScores = 0;
  81.         double scoreInFile;
  82.         double countOfScores = 0;
  83.  
  84.         // Read lines from the file until no more are left.
  85.         while (fileToReadScanner.hasNext()) {
  86.             // Read the next line
  87.             String value = fileToReadScanner.nextLine();
  88.  
  89.             // Display the last line read
  90.             System.out.printf("\n\tStudent %,d  :  %s\n", index, value);
  91.             index++; // increment Student # for printing purposes
  92.             scoreInFile = Double.parseDouble(value); // parse integer values in lines for average
  93.             // calculation
  94.             countOfScores++; // increment number of scores for average calculation
  95.             sumOfScores += scoreInFile; // get sum of all scores
  96.         }
  97.  
  98.  
  99.         System.out.printf("\n\tClass Test Score Average  :  %,.2f",
  100.                 (sumOfScores/countOfScores));
  101.         System.out.print("\n\nEND FILE.\n\n");
  102.         System.out.println("____________________________________________________________");
  103.         fileToReadScanner.close();
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement