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