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