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