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