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