Guest User

Untitled

a guest
Mar 10th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. package average;
  2. import java.util.Scanner;
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.lang.SecurityException;
  8. import java.nio.file.NoSuchFileException;
  9. import java.util.Formatter;
  10. import java.util.FormatterClosedException;
  11. import java.util.NoSuchElementException;
  12. import java.util.InputMismatchException;
  13.  
  14.  
  15. public class Average {
  16.  
  17. private static Formatter output;
  18.  
  19. static Scanner input = new Scanner(System.in);
  20. static int[] numbers = new int[10];
  21.  
  22. public static void main(String[] args)
  23. {
  24. openFile();
  25. addRecords();
  26. closeFile();
  27. readRecords();
  28. }
  29. public static void openFile()
  30. {
  31. try
  32. {
  33. output = new Formatter("numbers.txt");
  34. }
  35. catch (SecurityException securityException)
  36. {
  37. System.err.println("Write permission denied. Terminating.");
  38. System.exit(1);
  39. }
  40. catch (FileNotFoundException fileNotFoundException)
  41. {
  42. System.err.println("Error opening file. Terminating.");
  43. System.exit(1);
  44. }
  45. }
  46. public static void addRecords()
  47. {
  48. System.out.print("Hello, welcome to my magical program!\n");
  49.  
  50. for (int i = 0 ; i < 10 ; i++) {
  51. System.out.printf("Please enter integer no. %d: ", i+1);
  52. numbers[i] = input.nextInt();
  53. System.out.println();
  54. {
  55. try
  56. {
  57. output.format("Inputted integer: %s%n", String.valueOf(numbers[i]));
  58. }
  59. catch (FormatterClosedException formatterClosedexception)
  60. {
  61. System.err.println("Error writing to the file. Terminating.");
  62. break;
  63. }
  64. catch (InputMismatchException inputMismatchException)
  65. {
  66. System.err.println("Please restart the program and enter integers ONLY.");
  67. break;
  68. }
  69. catch (NoSuchElementException elementException)
  70. {
  71. System.err.println("Invalid input. Please try again.");
  72. input.nextLine();
  73. }
  74. }
  75. }
  76. }
  77. public static void closeFile(){
  78. {
  79. if (output != null)
  80. output.close();
  81. }
  82. }
  83. public static void readRecords()
  84. {
  85. try (BufferedReader br = new BufferedReader (new FileReader("numbers.txt"))){
  86.  
  87. String line;
  88. int[] number = new int[10];
  89. int i=-1;
  90. int sum = 0;
  91. double average = 0;
  92.  
  93. while ((line = br.readLine()) != null) {
  94. i++;
  95. String[] split = line.split(":");
  96. line = split[1].trim();
  97. number[i] = Integer.parseInt(line);
  98. System.out.printf("Integer number %d: %d%n", i, numbers[i]);
  99. sum += number[i];
  100. average = (double) sum/10;
  101. }
  102.  
  103. System.out.printf("%nWould your sum happen to be %d? %n", sum);
  104. System.out.printf("Which means your average is: %.2f %n", average);
  105.  
  106. }
  107. catch (NoSuchFileException noSuchFileException)
  108. {
  109. System.out.print("This file was not created properly and cannot be found.");
  110. }
  111. catch (FileNotFoundException fileNotFoundException)
  112. {
  113. System.out.print("I can't seem to find your file :( That's too bad...");
  114. }
  115. catch (IOException ioexception)
  116. {
  117. System.out.print("Whoopsie daisy, you got yourself an IOException. Better luck next time!");
  118. }
  119. finally
  120. {
  121. System.out.print("Check your numbers.txt file and see what ya got!");
  122. }
  123. }
  124. }
Add Comment
Please, Sign In to add comment