WordsList.java
1    /* 
2     * Program featuring an array to store and interactively manipulate a list of words. 
3     */
4    package arrayplay;
5    
6    import java.io.File;
7    import java.io.FileNotFoundException;
8    import java.util.Scanner;
9    
10   public class WordsList {
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   
17       public static void main(String[] args) {
18           try {
19               // ESTABLISH THE ARRAY OF words
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       // Assuming that the data file will be found in the public_html/data
32       // subdirectory of the user’s home directory.
33       private static Scanner establishScanner(String fn) throws FileNotFoundException {
34           String separator = File.separator;
35           String homeDirectory = System.getProperty("user.home");
36           String path = homeDirectory + separator + "public_html" + separator + "data" + separator;
37           String fullFileName = path + fn;
38           return new Scanner(new File(fullFileName));
39       }
40   
41       private static void readWords() throws FileNotFoundException {
42           Scanner scanner = establishScanner("WordSet.txt");
43           while (scanner.hasNext()) {
44               words[numberOfWords] = scanner.next();
45               numberOfWords = numberOfWords + 1;
46           }
47       }
48   
49       private static void displayWords() {
50           for (int x = 0; x < numberOfWords; x = x + 1) {
51               System.out.println(words[x]);
52           }
53       }
54   
55       private static void interpreter() {
56           System.out.print(">>> ");
57           String command = commandReader.next();
58           if (command.equalsIgnoreCase("DISPLAY")) {
59               interpreterDisplayCommand();
60           } else if (command.equalsIgnoreCase("PRINT")) {
61               interpretPrintCommand();
62           } else if (command.equalsIgnoreCase("SWAP")) {
63               interpretSwapCommand();
64           } else if (command.equalsIgnoreCase("ADD")) {
65               interpretAddCommand();
66           } else if (command.equalsIgnoreCase("HELP")) {
67               interpretHelpCommand();
68           } else if (command.equalsIgnoreCase("EXIT")) {
69               System.exit(0);
70           } else {
71               System.out.println("### Unrecognizable command: " + command);
72           }
73           interpreter();
74       }
75   
76       private static void interpreterDisplayCommand() {
77           displayWords();
78       }
79   
80       private static void interpretPrintCommand() {
81           String operand = commandReader.next();
82           if (operand.equalsIgnoreCase("FIRST")) {
83               System.out.println(words[0]);
84           } else if (operand.equalsIgnoreCase("LAST")) {
85               System.out.println(words[numberOfWords - 1]);
86           } else {
87               int index = Integer.valueOf(operand);
88               System.out.println(words[index - 1]);
89           }
90       }
91   
92       private static void interpretHelpCommand() {
93           System.out.println("HELP - display a menu of commands");
94           System.out.println("DISPLAY - display the list of numbers");
95           System.out.println("PRINT - print a number (FIRST;LAST;nth)");
96           System.out.println("SWAP - exchange two elements (nth;mth)");
97           System.out.println("ADD - add a number to the list (FIRST;LAST)");
98           System.out.println("EXIT - terminate execution of the program");
99       }
100  
101      private static void interpretSwapCommand() {
102          int position1 = commandReader.nextInt();
103          int position2 = commandReader.nextInt();
104          String temp = words[position1 - 1];
105          words[position1 - 1] = words[position2 - 1];
106          words[position2 - 1] = temp;
107      }
108  
109      private static void interpretAddCommand() {
110          String position = commandReader.next();
111          if (position.equalsIgnoreCase("LAST")) {
112              addLast();
113          } else if (position.equalsIgnoreCase("FIRST")) {
114              addFirst();
115          } else {
116              System.out.println("### invalid operand for add command");
117          }
118          numberOfWords = numberOfWords + 1;
119      }
120  
121      private static void addLast() {
122          words[numberOfWords] = commandReader.next();
123      }
124  
125      private static void addFirst() {
126          for (int x = numberOfWords; x > 0; x = x - 1) {
127              words[x] = words[x - 1];
128          }
129          words[0] = commandReader.next();
130      }
131  }