Advertisement
Guest User

Untitled

a guest
Mar 8th, 2016
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. package average;
  2. import java.util.Scanner;
  3. import java.io.FileNotFoundException;
  4. import java.lang.SecurityException;
  5. import java.util.Formatter;
  6. import java.util.FormatterClosedException;
  7. import java.util.NoSuchElementException;
  8.  
  9. public class Average {
  10.  
  11. private static Formatter output;
  12.  
  13. public static void main(String[] args)
  14. {
  15. openFile();
  16. addRecords();
  17. closeFile();
  18. }
  19. public static void openFile()
  20. {
  21. try
  22. {
  23. output = new Formatter("numbers.txt");
  24. }
  25. catch (SecurityException securityException)
  26. {
  27. System.err.println("Write permission denied. Terminating.");
  28. System.exit(1);
  29. }
  30. catch (FileNotFoundException fileNotFoundException)
  31. {
  32. System.err.println("Error opening file. Terminating.");
  33. System.exit(1);
  34. }
  35. }
  36. public static void addRecords()
  37. {
  38. Scanner input = new Scanner(System.in);
  39. System.out.print("Hello, welcome to my program!\n");
  40.  
  41. int[] numbers = new int[10];
  42.  
  43. for (int i = 0 ; i < 10 ; i++) {
  44. System.out.printf("Please enter integer %d: ", i+1);
  45. numbers[i] = input.nextInt();
  46.  
  47. {
  48. try
  49. {
  50. output.format("Integer inputted: %s%n", String.valueOf(input));
  51. }
  52. catch (FormatterClosedException formatterClosedexception)
  53. {
  54. System.err.println("Error writing to the file. Terminating.");
  55. break;
  56. }
  57. catch (NoSuchElementException elementException)
  58. {
  59. System.err.println("Invalid input. Please try again.");
  60. input.nextLine();
  61. }
  62.  
  63. //System.out.print("? ");
  64. }
  65. }
  66. }
  67. public static void closeFile()
  68. {
  69. if (output != null)
  70. output.close();
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement