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