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