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