C:\Users\notebook\Documents\NetBeansProjects\CS2\src\textprocessing\Reverse.java
 1 /*
 2  * Program to read text from a selected file and print the text in a 
 3  * reverse order.
 4  */
 5 package textprocessing;
 6 
 7 import java.io.File;
 8 import java.io.FileNotFoundException;
 9 import java.io.IOException;
10 import java.util.ArrayList;
11 import java.util.Scanner;
12 import javax.swing.JFileChooser;
13  
14 /**
15  *
16  * @author kchan2
17  */
18 
19 public class Reverse {
20 
21     /**
22      * @param args the command line arguments
23      */
24     public static void main(String[] args) throws FileNotFoundException {
25         Scanner scanner = equateScannerWithFile();
26         ArrayList<String> words = readWordsFromFile(scanner);
27         printReverse(words);
28     }
29 
30     private static ArrayList<String> readWordsFromFile(Scanner scanner) throws FileNotFoundException {
31         
32         ArrayList<String> words = new ArrayList<>();
33         while ( scanner.hasNext() ) {
34             String word = scanner.next();
35             words.add(word);
36         }
37         return words;
38     }
39 
40     private static void printReverse(ArrayList<String> words) {
41         for ( int x = words.size()-1; x >= 0; x = x - 1) {
42             System.out.println(words.get(x));
43         }
44     }
45 
46     private static Scanner equateScannerWithFile() throws FileNotFoundException {
47         String homedir = System.getProperty("user.home");
48         JFileChooser jfc = new JFileChooser(new File(homedir));
49         jfc.showOpenDialog(null);
50         File file = jfc.getSelectedFile();
51         Scanner scanner = new Scanner(file);
52         return scanner;
53     }
54  
55 }