Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package average;
- import java.util.Scanner;
- import java.io.FileNotFoundException;
- import java.lang.SecurityException;
- import java.util.Formatter;
- import java.util.FormatterClosedException;
- import java.util.NoSuchElementException;
- import java.util.InputMismatchException;
- public class Average {
- private static Formatter output;
- public static void main(String[] args)
- {
- openFile();
- addRecords();
- closeFile();
- openFile();
- readRecords();
- closeFile();
- }
- 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()
- {
- Scanner input = new Scanner(System.in);
- System.out.print("Hello, welcome to my program!\n");
- int[] numbers = new int[10];
- for (int i = 0 ; i < 10 ; i++) {
- System.out.printf("Please enter integer %d: ", i+1);
- numbers[i] = input.nextInt();
- {
- 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();
- }
- //System.out.print("? ");
- }
- }
- }
- public static void closeFile()
- {
- if (output != null)
- output.close();
- }
- public static void readRecords()
- {
- try
- {
- while (input.hasNext())
- {
- System.out.printf("%s%n", String.valueOf(numbers[i]));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement