Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class ClassAverages {
- public static void main(String[] args) throws FileNotFoundException {
- Scanner scan = new Scanner(System.in);
- System.out.print("\nEnter the filename: ");
- String filename = scan.nextLine();
- filename += ".txt";
- System.out.println("\nCalling \"inputScores()\" method...");
- inputScores(filename);
- System.out.println("\nCalling \"outputScores()\" method...");
- outputScores(filename);
- } // end main()
- private static void inputScores(String someFileName) throws FileNotFoundException {
- Scanner scan = new Scanner(System.in);
- int studentNumber = 1;
- try {
- PrintWriter outputFile = new PrintWriter(someFileName); // create an instance of the
- // PrintWriter class to write data to file
- System.out.printf("\nEnter score for user %,d: (enter -1 to exit): ", studentNumber);
- double score = scan.nextDouble();
- System.out.println(); // spacing for readability
- while (score < -1.0) {
- System.out.printf("A score of %,.2f is invalid.", score);
- System.out.printf("\nEnter score for user %,d: (enter -1 to exit): ", studentNumber);
- score = scan.nextInt();
- System.out.println(); // spacing for readability
- }
- while (score != -1) {
- outputFile.println(score); // println method writes a line of data to a file.
- studentNumber++; // increment Student #
- System.out.printf("Enter score for user %,d: (enter -1 to exit): ", studentNumber);
- score = scan.nextDouble();
- System.out.println(); // spacing for readability
- while (score < -1.0) {
- System.out.printf("A score of %,.2f is invalid.", score);
- System.out.printf("\nEnter score for user %,d: (enter -1 to exit): ", studentNumber);
- score = scan.nextDouble();
- }
- }
- System.out.println("All values entered have been written to the file " +
- "\"" + someFileName + "\"");
- outputFile.close();
- scan.close();
- } catch (InputMismatchException e) {
- System.out.println("\nInvalid input detected. \nEnding program now.\nGoodbye!");
- //System.out.println("ERROR: " + e.toString());
- System.exit(0);
- }
- } // end inputScores()
- private static void outputScores(String someFileName) throws FileNotFoundException {
- System.out.println("____________________________________________________________");
- System.out.printf("\n\nOPENING FILE \"%s\"\n", someFileName);
- // creates an instance of the File class
- // Pass String parameter to the File object constructor
- File fileToRead = new File(someFileName);
- if (!fileToRead.exists()) { // Not necessary but for safe-handling, just in case
- System.out.println("\nFile does not exist.");
- System.exit(0);
- }
- // Pass a reference to this File object as an argument to the Scanner class constructor
- // This creates a Scanner object that uses the file someFileName as its source of input.
- Scanner fileToReadScanner = new Scanner(fileToRead);
- int index = 1;
- double sumOfScores = 0;
- double scoreInFile;
- double countOfScores = 0;
- // Read lines from the file until no more are left.
- while (fileToReadScanner.hasNext()) {
- // Read the next line
- String value = fileToReadScanner.nextLine();
- // Display the last line read
- System.out.printf("\n\tStudent %,d : %s\n", index, value);
- index++; // increment Student # for printing purposes
- scoreInFile = Double.parseDouble(value); // parse integer values in lines for average
- // calculation
- countOfScores++; // increment number of scores for average calculation
- sumOfScores += scoreInFile; // get sum of all scores
- }
- System.out.printf("\n\tClass Test Score Average : %,.2f",
- (sumOfScores/countOfScores));
- System.out.print("\n\nEND FILE.\n\n");
- System.out.println("____________________________________________________________");
- fileToReadScanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement