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   
17          public static void main(String[] args) {
18              try {
19                  // ESTABLISH THE ARRAY OF NUMBERS
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 try 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("WordList.txt");
43              while (scanner.hasNext()) {
44                  words[numberOfWords] = scanner.nextLine();
45                  numberOfWords = numberOfWords + 1;
46              }
47          }
48          private static void displayNumbers() {
49              for (int x = 0; x < numberOfWords; x++) {
50                  System.out.println(words[x]);
51              }
52          }
53          private static void interpreter() {
54              System.out.print(">>> ");
55              String command = commandReader.next();
56              if (command.equalsIgnoreCase("DISPLAY")) {
57                  interpreterDisplayCommand();
58              } else if (command.equalsIgnoreCase("PRINT")) {
59                  interpretPrintCommand();
60              } else if (command.equalsIgnoreCase("SWAP")) {
61                  interpretSwapCommand();
62              } else if (command.equalsIgnoreCase("ADD")) {
63                  interpretAddCommand();
64              } else if (command.equalsIgnoreCase("HELP")) {
65                  interpretHelpCommand();
66              } else if (command.equalsIgnoreCase("EXIT")) {
67                  System.exit(0);
68              } else {
69                  System.out.println("### Unrecognizable command: " + command);
70              }
71              interpreter();
72          }
73          private static void interpreterDisplayCommand() {
74              displayNumbers();
75          }
76   
77          private static void interpretPrintCommand() {
78              String operand = commandReader.next();
79              if (operand.equalsIgnoreCase("FIRST")) {
80                  System.out.println(words[0]);
81              } else if (operand.equalsIgnoreCase("LAST")) {
82                  System.out.println(words[numberOfWords - 1]);
83              } else {
84                  int index = Integer.valueOf(operand);
85                  System.out.println(words[index - 1]);
86              }
87          }
88   
89          private static void interpretHelpCommand() {
90              System.out.println("HELP - display a menu of commands");
91              System.out.println("DISPLAY - display the list of numbers");
92              System.out.println("PRINT - print a number (FIRST;LAST;nth)");
93              System.out.println("SWAP - exchange two elements (nth;mth)");
94              System.out.println("ADD - add a number to the list (FIRST;LAST)");
95              System.out.println("EXIT - terminate execution of the program");
96         }
97   
98         private static void interpretSwapCommand() {
99             int position1 = commandReader.nextInt();
100            int position2 = commandReader.nextInt();
101            String temp = words[position1 - 1];
102            words[position1 - 1] = words[position2 - 1];
103            words[position2 - 1] = temp;
104        }
105        private static void interpretAddCommand() {
106            String position = commandReader.next();
107            if (position.equalsIgnoreCase("LAST")) {
108                addLast();
109            } else if (position.equalsIgnoreCase("FIRST")) {
110                addFirst();
111            } else {
112                System.out.println("### invalid operand for add command");
113            }
114            numberOfWords = numberOfWords + 1;
115        }
116  
117        private static void addLast() {
118             words[numberOfWords] = commandReader.nextLine();
119        }
120        private static void addFirst() {
121            for (int x = numberOfWords; x > 0; x--) {
122                words[x] = words[x - 1];
123            }
124            words[0] = commandReader.nextLine();
125        }
126     }