WordList.java
1    /* 
2     *Program featuring an array to store and interactively manipulate a list of numbers. 
3     */
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<>();
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 + "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("WordSet.txt");
45           while (scanner.hasNext()){
46               words.add( scanner.next());
47   
48           }
49       }
50   
51       private static void displayWords(){
52           for (int x = 0; x < words.size(); x=x+1){
53               System.out.println(words.get(x));
54           }
55       }
56   
57       private static void interpreter() {
58           System.out.print(">>> ");
59           String command =commandReader.next();
60           if(command.equalsIgnoreCase("display")){
61               interpreterDisplayCommand();
62           }else if (command.equalsIgnoreCase("print")){
63               interpretPrintCommand();
64           }else if (command.equalsIgnoreCase("swap")){
65               interpretSwapCommand();
66           }else if (command.equalsIgnoreCase("add")){
67               interpretAddCommand();
68           }else if (command.equalsIgnoreCase("help")){
69               interpretHelpCommand();
70           }else if (command.equalsIgnoreCase("exit")){
71               System.exit(0);
72           }else {
73               System.out.println("### Unrecognizable command" + command);
74           }
75           interpreter();
76       }
77       private static void interpreterDisplayCommand(){
78           displayWords();
79       }
80       private static void interpretPrintCommand(){
81           String operand = commandReader.next();
82           if(operand.equalsIgnoreCase("first")){
83               System.out.println(words.get(0));
84           }else if (operand.equalsIgnoreCase("last")){
85               System.out.println(words.get(words.size() - 1));
86           }else {
87               int index = Integer.valueOf(operand);
88               System.out.println(words.get(index - 1));
89           }
90           interpreter();
91       }
92   
93       private static void interpretHelpCommand(){
94           System.out.println("HELP - display a menu of commands");
95           System.out.println("DISPLAY - display the list of numbers");
96           System.out.println("PRINT - print a number(FIRST;LAST;nth)");
97           System.out.println("SWAP - exchange two elements (nth;mth)");
98           System.out.println("ADD - add a number to the list(FIRST;LAST)");
99           System.out.println("EXIT - terminate execution of the program");
100      }
101  
102      private static void interpretSwapCommand(){
103          int position1 = commandReader.nextInt();
104          int position2 = commandReader.nextInt();
105          String temp = words.get(position1 - 1);
106          words.set(position1 - 1, words.get(position2 - 1));
107          words.set(position2 - 1, temp);
108  
109      }
110  
111      private static void interpretAddCommand(){
112          String position = commandReader.next();
113          if(position.equalsIgnoreCase("last")){
114              addLast();
115          }else if (position.equalsIgnoreCase("first")){
116              addFirst();
117          }else {
118              System.out.println("### invalid operand for add command");
119          }
120  
121      }
122      private static void addLast(){
123          words.add(commandReader.next());
124      }
125      private static void addFirst(){
126  //        for (int x = words.size(); x > 0; x=x-1){
127  //            words.set(x, words.get(x - 1));
128  //        }
129          words.add(0, commandReader.next());
130      }
131  }
132