WordList.java
1    package arraylist;
2    
3    import java.io.File;
4    import java.io.FileNotFoundException;
5    import java.util.ArrayList;
6    import java.util.Scanner;
7    
8    public class WordList {
9        private static final int LIMIT = 1000;
10       private static ArrayList<String> words = new ArrayList<>(LIMIT);
11       private static int numberOfWords = 0;
12       private static Scanner commandReader = new Scanner(System.in);
13   
14       public static void main(String[] args) {
15           try {
16               readwords();
17               System.out.println("nThe original list of word ...");
18               displayWords();
19               interpreter();
20           } catch (FileNotFoundException ex) {
21               System.out.println("The file was not found. Please think again.");
22               System.exit(-1);
23           }
24       }
25   
26       private static Scanner establishScanner(String fn) throws FileNotFoundException {
27           String separator = File.separator;
28           String homeDirectory = System.getProperty("user.home");
29           String path = homeDirectory + separator + "public_html" + separator + "CS1WorkSite" + separator + "arraylist" + separator;
30           String fullFileName = path + fn;
31           return new Scanner(new File(fullFileName));
32       }
33   
34       private static void readwords() throws FileNotFoundException {
35           Scanner scanner = establishScanner("WordSet.txt");
36           while (scanner.hasNext()) {
37               String word = scanner.next();
38               words.add(word);
39           }
40       }
41   
42       private static void displayWords() {
43           int numberOfWords = words.size();
44           for (int x = 0; x < numberOfWords; x = x + 1) {
45               System.out.println(words.get(x));
46           }
47       }
48   
49       private static void interpreter() {
50           System.out.print(">>> ");
51           String command = commandReader.next();
52           if (command.equalsIgnoreCase("DISPLAY")) {
53               interpreterDisplayCommand();
54           } else if (command.equalsIgnoreCase("PRINT")) {
55               interpretPrintCommand();
56           } else if (command.equalsIgnoreCase("SWAP")) {
57               interpretSwapCommand();
58           } else if (command.equalsIgnoreCase("ADD")) {
59               interpretAddCommand();
60           } else if (command.equalsIgnoreCase("HELP")) {
61               interpretHelpCommand();
62           } else if (command.equalsIgnoreCase("EXIT")) {
63               System.exit(0);
64           } else {
65               System.out.println("### Unrecognizable command: " + command);
66           }
67           interpreter();
68       }
69   
70       private static void interpreterDisplayCommand() {
71           displayWords();
72       }
73   
74       private static void interpretPrintCommand() {
75           String operand = commandReader.next();
76           if (operand.equalsIgnoreCase("FIRST")) {
77               System.out.println(words.get(0));
78           } else if (operand.equalsIgnoreCase("LAST")) {
79               System.out.println(words.get(words.size()-1));
80           } else {
81               int index = Integer.valueOf(operand);
82               System.out.println(words.get(index - 1));
83           }
84       }
85   
86       private static void interpretHelpCommand() {
87           System.out.println("HELP - display a menu of commands");
88           System.out.println("DISPLAY - display the list of numbers");
89           System.out.println("PRINT - print a word (FIRST;LAST;nth)");
90           System.out.println("SWAP - exchange two elements (nth;mth)");
91           System.out.println("ADD - add a word to the list (FIRST;LAST)");
92           System.out.println("EXIT - terminate execution of the program");
93       }
94   
95       private static void interpretSwapCommand() {
96           int position1 = commandReader.nextInt();
97           int position2 = commandReader.nextInt();
98           String temp = words.get(position1 - 1);
99           words.set(position1 - 1, words.get(position2 - 1));
100          words.set(position2 - 1, temp);
101      }
102  
103      private static void interpretAddCommand() {
104          String position = commandReader.next();
105          if (position.equalsIgnoreCase("LAST")) {
106              addLast();
107          } else if (position.equalsIgnoreCase("FIRST")) {
108              addFirst();
109          } else {
110              System.out.println("### invalid operand for add command");
111          }
112          numberOfWords = numberOfWords + 1;
113      }
114  
115      private static void addLast() {
116          words.add(words.size(), commandReader.next());
117      }
118  
119      private static void addFirst() {
120          words.add(0 , commandReader.next());
121      }
122  }
123