WordList.java
1    package arraylist;
2    
3    import java.io.File;
4    import java.io.FileNotFoundException;
5    import java.util.ArrayList;
6    import java.util.Scanner;
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> Words = new ArrayList<String>(1000);
12       private static int numberOfStrings = 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               readWord();
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 readWord() throws FileNotFoundException {
41           Scanner scanner = establishScanner("WordSet.txt");
42           while (scanner.hasNext()) {
43               Words.add(scanner.next());
44           }
45       }
46   
47       private static void displayNumbers() {
48           for (int x = 0; x < Words.size(); x = x + 1) {
49              System.out.println(Words.get(x));
50           }
51   
52           }
53   
54   
55       private static void interpreter() {
56           System.out.print(">>> ");
57           String command = commandReader.next();
58           if (command.equalsIgnoreCase("DISPLAY")) {
59               interpreterDisplayCommand();
60           } else if (command.equalsIgnoreCase("PRINT")) {
61               interpretPrintCommand();
62           } else if (command.equalsIgnoreCase("SWAP")) {
63               interpretSwapCommand();
64           } else if (command.equalsIgnoreCase("ADD")) {
65               interpretAddCommand();
66           } else if (command.equalsIgnoreCase("HELP")) {
67               interpretHelpCommand();
68           } else if (command.equalsIgnoreCase("EXIT")) {
69               System.exit(0);
70           } else {
71               System.out.println("### Unrecognizable command: " + command);
72           }
73           interpreter();
74       }
75   
76       private static void interpreterDisplayCommand() {
77           displayNumbers();
78       }
79   
80       private static void interpretPrintCommand() {
81           String operand = commandReader.next();
82           if (operand.equalsIgnoreCase("FIRST")) {
83               System.out.println(Words.listIterator(0));
84           } else if (operand.equalsIgnoreCase("LAST")) {
85               System.out.println(Words.listIterator(numberOfStrings - 1));
86           } else {
87               int index = Integer.valueOf(operand);
88               System.out.println(Words.listIterator(index -1));
89           }
90       }
91   
92       private static void interpretHelpCommand() {
93           System.out.println("HELP - display a menu of commands");
94           System.out.println("DISPLAY - display the list of numbers");
95           System.out.println("PRINT - print a number (FIRST;LAST;nth)");
96           System.out.println("SWAP - exchange two elements (nth;mth)");
97           System.out.println("ADD - add a number to the list (FIRST;LAST)");
98           System.out.println("EXIT - terminate execution of the program");
99       }
100  
101      private static void interpretSwapCommand() {
102          int position1 = commandReader.nextInt();
103          int position2 = commandReader.nextInt();
104          //String temp = Words[position1 - 1];
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      private static void interpretAddCommand() {
111          String position = commandReader.next();
112          if (position.equalsIgnoreCase("LAST")) {
113              addLast();
114          } else if (position.equalsIgnoreCase("FIRST")) {
115              addFirst();
116          } else {
117              System.out.println("### invalid operand for add command");
118          }
119      }
120  
121      private static void addLast() {
122          Words.add(commandReader.next());
123      }
124  
125      private static void addFirst() {
126  
127          Words.add(0, commandReader.next());
128      }
129  }
130  
131  
132