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