WordArraylist.java
1    package arraylistplay;
2    
3    import java.io.File;
4    import java.io.FileNotFoundException;
5    import java.util.ArrayList;
6    import java.util.Collections;
7    import java.util.List;
8    import java.util.Scanner;
9    
10   public class WordArraylist {
11   
12   
13       private static final int LIMIT = 1000;
14       private static List<String> words = new ArrayList<>();
15       private static int numberOfWords = words.size();
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   
41           System.out.println(fullFileName);
42           System.out.println(words.size());
43           return new Scanner(new File(fullFileName));
44       }
45   
46       private static void readNumbers() throws FileNotFoundException {
47           Scanner scanner = establishScanner("WordSet.txt");
48           while (scanner.hasNext()) {
49               words.add(scanner.next());
50               numberOfWords = numberOfWords + 1;
51           }
52       }
53   
54       private static void displayNumbers() {
55           for (int x = 0; x < numberOfWords; x = x + 1) {
56               System.out.println(words.get(x));
57           }
58       }
59   
60       private static void interpreter() {
61           System.out.print(">>> ");
62           String command = commandReader.next();
63           if (command.equalsIgnoreCase("DISPLAY")) {
64               interpreterDisplayCommand();
65           } else if (command.equalsIgnoreCase("PRINT")) {
66               interpretPrintCommand();
67           } else if (command.equalsIgnoreCase("SWAP")) {
68               interpretSwapCommand();
69           } else if (command.equalsIgnoreCase("ADD")) {
70               interpretAddCommand();
71           } else if (command.equalsIgnoreCase("HELP")) {
72               interpretHelpCommand();
73           } else if (command.equalsIgnoreCase("EXIT")) {
74               System.exit(0);
75           } else {
76               System.out.println("### Unrecognizable command: " + command);
77           }
78           interpreter();
79       }
80   
81       private static void interpreterDisplayCommand() {
82           displayNumbers();
83       }
84   
85       private static void interpretPrintCommand() {
86           String operand = commandReader.next();
87           if (operand.equalsIgnoreCase("FIRST")) {
88               System.out.println(words.get(0));
89           } else if (operand.equalsIgnoreCase("LAST")) {
90               System.out.println(words.get(numberOfWords - 1));
91           } else {
92               int index = Integer.valueOf(operand);
93               System.out.println(words.get(index - 1));
94           }
95       }
96   
97       private static void interpretHelpCommand() {
98           System.out.println("HELP - display a menu of commands");
99           System.out.println("DISPLAY - display the list of numbers");
100          System.out.println("PRINT - print a number (FIRST;LAST;nth)");
101          System.out.println("SWAP - exchange two elements (nth;mth)");
102          System.out.println("ADD - add a number to the list (FIRST;LAST)");
103          System.out.println("EXIT - terminate execution of the program");
104      }
105  
106      private static void interpretSwapCommand() {
107  
108          int position1 = commandReader.nextInt();
109          int position2 = commandReader.nextInt();
110          String temp = words.get(position1 - 1);
111          String temp2 = words.get(position2 - 1);
112          words.set(position1 - 1, temp2);
113          words.set(position2 - 1, temp);
114  
115  
116      }
117  
118      private static void interpretAddCommand() {
119          String position = commandReader.next();
120          if (position.equalsIgnoreCase("LAST")) {
121              addLast();
122          } else if (position.equalsIgnoreCase("FIRST")) {
123              addFirst();
124          } else {
125              System.out.println("### invalid operand for add command");
126          }
127          numberOfWords = numberOfWords + 1;
128      }
129  
130      private static void addLast() {
131          words.add(String.valueOf(commandReader.next()));
132      }
133  
134      private static void addFirst() {
135  
136          words.add(0, String.valueOf(commandReader.next()));
137      }
138  
139  
140  }
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155