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