public class Reverse600EasyWay
1: import java.io.File;
2: import java.io.FileNotFoundException;
3: import java.io.PrintWriter;
4: import java.util.ArrayList;
5: import java.util.List;
6: import java.util.Scanner;
8: /**
9: * This program reverses 600 numbers from a file.
10: * It uses an array to make the code much shorter than it would be
11: * without an array (for which see Reverse600HardWay.java).
12: *
13: * @author Mark Young (A00000000)
14: */
15: public class Reverse600EasyWay {
17: /** the number of numbers I will reverse */
18: public static final int SIX_HUNDRED = 600;
19: public static final int MAX_TRIES = 3;
20: public static final Scanner KBD = new Scanner(System.in);
22: public static void main(String[] args) {
23: // create variables
24: Scanner fileInput;
25: List<Integer> n = new ArrayList<>();
26: PrintWriter fileOutput;
28: // introduce yourself
29: System.out.print("\n\n"
30: + "A program to reverse "
31: + "numbers read from a file.\n\n");
33: // get the file names
34: fileInput = getTheScanner();
35: fileOutput = getTheWriter();
37: // read the numbers
38: System.out.println("Reading the numbers now:\n");
39: int numElements = 0;
40: while (fileInput.hasNextInt()) {
41: n.add(fileInput.nextInt());
42: ++numElements;
43: }
44: fileInput.close();
46: // print them out in reverse order
47: for (int i = numElements-1; i >= 0; i--) {
48: fileOutput.println(n.get(i));
49: }
50: fileOutput.close();
51:
52: System.out.println("\n\nThey are In reverse order in your file.\n");
54: // print some blank lines to make the ending less abrupt
55: System.out.print("\n\n");
56: }
57:
58: /**
59: * Create a Scanner connected to a file chosen by the user.
60: * User gets MAX_TRIES attempts to name a suitable file.
61: *
62: * @return a Scanner connected to a user-chosen file
63: */
64: private static Scanner getTheScanner() {
65: for (int i = 1; i <= MAX_TRIES; ++i) {
66: System.out.print("Please enter the name of the input file: ");
67: String fileName = KBD.nextLine();
68: File theFile = new File(fileName);
69: try {
70: return new Scanner(theFile);
71: } catch (FileNotFoundException ex) {
72: System.out.println("Could not open " + fileName + " for input");
73: }
74: }
75: System.err.println("Too many failed names. Quitting.");
76: System.exit(1);
77: return null;
78: }
80: /**
81: * Create a PrintWriter connected to a file chosen by the user.
82: * User gets MAX_TRIES attempts to name a suitable file.
83: *
84: * @return a PrintWriter connected to a user-chosen file
85: */
86: private static PrintWriter getTheWriter() {
87: for (int i = 1; i <= MAX_TRIES; ++i) {
88: System.out.print("Please enter the name of the output file: ");
89: String fileName = KBD.nextLine();
90: File theFile = new File(fileName);
91: try {
92: return new PrintWriter(theFile);
93: } catch (FileNotFoundException ex) {
94: System.out.println("Could not open " + fileName + " for output");
95: }
96: }
97: System.err.println("Too many failed names. Quitting.");
98: System.exit(2);
99: return null;
100: }
102: }