Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package average;
- import java.util.Scanner;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.lang.SecurityException;
- import java.nio.file.NoSuchFileException;
- import java.util.Formatter;
- import java.util.FormatterClosedException;
- import java.util.NoSuchElementException;
- import java.util.InputMismatchException;
- public class Average {
- private static Formatter output;
- static Scanner input = new Scanner(System.in);
- static int[] numbers = new int[10];
- public static void main(String[] args)
- {
- openFile();
- addRecords();
- closeFile();
- readRecords();
- }
- public static void openFile()
- {
- try
- {
- output = new Formatter("numbers.txt");
- }
- catch (SecurityException securityException)
- {
- System.err.println("Write permission denied. Terminating.");
- System.exit(1);
- }
- catch (FileNotFoundException fileNotFoundException)
- {
- System.err.println("Error opening file. Terminating.");
- System.exit(1);
- }
- }
- public static void addRecords()
- {
- System.out.print("Hello, welcome to my magical program!\n");
- for (int i = 0 ; i < 10 ; i++) {
- System.out.printf("Please enter integer no. %d: ", i+1);
- numbers[i] = input.nextInt();
- System.out.println();
- {
- try
- {
- output.format("Inputted integer: %s%n", String.valueOf(numbers[i]));
- }
- catch (FormatterClosedException formatterClosedexception)
- {
- System.err.println("Error writing to the file. Terminating.");
- break;
- }
- catch (InputMismatchException inputMismatchException)
- {
- System.err.println("Please restart the program and enter integers ONLY.");
- break;
- }
- catch (NoSuchElementException elementException)
- {
- System.err.println("Invalid input. Please try again.");
- input.nextLine();
- }
- }
- }
- }
- public static void closeFile(){
- {
- if (output != null)
- output.close();
- }
- }
- public static void readRecords()
- {
- try (BufferedReader br = new BufferedReader (new FileReader("numbers.txt"))){
- String line;
- int[] number = new int[10];
- int i=-1;
- int sum = 0;
- double average = 0;
- while ((line = br.readLine()) != null) {
- i++;
- String[] split = line.split(":");
- line = split[1].trim();
- number[i] = Integer.parseInt(line);
- System.out.printf("Integer number %d: %d%n", i, numbers[i]);
- sum += number[i];
- average = (double) sum/10;
- }
- System.out.printf("%nWould your sum happen to be %d? %n", sum);
- System.out.printf("Which means your average is: %.2f %n", average);
- }
- catch (NoSuchFileException noSuchFileException)
- {
- System.out.print("This file was not created properly and cannot be found.");
- }
- catch (FileNotFoundException fileNotFoundException)
- {
- System.out.print("I can't seem to find your file :( That's too bad...");
- }
- catch (IOException ioexception)
- {
- System.out.print("Whoopsie daisy, you got yourself an IOException. Better luck next time!");
- }
- finally
- {
- System.out.print("Check your numbers.txt file and see what ya got!");
- }
- }
- }
Add Comment
Please, Sign In to add comment