WordList.java
1    /* 
2     * Program featuring an array to store and interactively manipulate a list of string 
3     */
4    
5    package arrayPlay;
6    
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       // new array called words
15       private static String[] words = new String [LIMIT];
16       private static int numberOfWords=0;
17       private static Scanner commandReader = new Scanner(System.in);
18   
19       public static void main(String[] args){
20           try{
21               // ESTABLISH THE ARRAY OF NUMBERS
22               readWords();
23               // CHECK THE DATA
24               // System.out.println("nThe original list of numbers..")
25               // displayNumber():
26               // ENTER THE INTERPRETER
27               interpreter();
28           } catch (FileNotFoundException ex) {
29               // If the file is not found, the system will catch it to tell the user
30               // to inform. then the system exit after 1 sec.
31               System.out.println("The file was not found. Please think again");
32               System.exit(-1);
33           }
34       }
35   
36       // Assuming the data file will be found in the public.html/data
37       // subdirectory of the user's home directory
38       private static Scanner establishScanner(String fn) throws FileNotFoundException{
39           String separator = File.separator;
40           String homeDirectory = System.getProperty("user.home");
41           // tell the system the direction of the file so its user/home/pubic_html/data
42           String path = homeDirectory + separator + "public_html" + separator + "CS1WorkSite" + separator + "arrayPlay" + separator;
43           String fullFileName = path + fn;
44           return new Scanner(new File(fullFileName));
45       }
46   
47       private static void readWords() throws FileNotFoundException {
48           // scanner will scan the new file in the directory given in the establishScanner methods
49           Scanner scanner = establishScanner("WordSet.txt");
50           // scanner.hasNext() is a boolean that is true if there is another token in the input.
51           while (scanner.hasNext()) {
52               // numberOfWords become 15 depending on the input file
53               words[numberOfWords] = scanner.next();
54               numberOfWords = numberOfWords + 1;
55           }
56       }
57   
58       private static void displayNumbers(){
59           // this will output everything added into the array
60           for (int x = 0; x < numberOfWords; x = x +1 ){
61               System.out.println(words[x]);
62           }
63       }
64   
65       private static void interpreter() {
66           System.out.print(">>> ");
67           // commandReader(scanner) will return the next string from the system input
68           String command = commandReader.next();
69           if (command.equalsIgnoreCase("DISPLAY")) {
70               interpreterDisplayCommand();
71           } else if (command.equalsIgnoreCase("PRINT")){
72               interpreterPrintCommmand();
73           } else if (command.equalsIgnoreCase("SWAP")){
74               interpreterSwapCommand();
75           } else if (command.equalsIgnoreCase("ADD")) {
76               interpreterAddCommand();
77           } else if (command.equalsIgnoreCase("HELP")) {
78               interpreterHelpCommand();
79           } else if (command.equalsIgnoreCase("EXIT")) {
80               System.exit(0);
81           } else {
82               System.out.println("### Unregonizable Command: " + command);
83           }
84           interpreter();
85       }
86   
87       private static void interpreterDisplayCommand() {
88           // Can't we just write this before since it's only one command????
89           displayNumbers();
90       }
91   
92       private static void interpreterPrintCommmand() {
93           String operand = commandReader.next();
94           if (operand.equalsIgnoreCase("First")){
95               System.out.println(words[0]);
96           } else if (operand.equalsIgnoreCase("LAST")){
97               System.out.println(words[numberOfWords-1]);
98           } else {
99               // "Integer.valueOf(operand)" convert the string operand to an integer. Super useful
100              int index = Integer.valueOf(operand);
101              System.out.println(words[index-1]);
102          }
103      }
104  
105      private static void interpreterHelpCommand() {
106          System.out.println("HELP - display a menu of commands");
107          System.out.println("DISPLAY - display the list of words");
108          System.out.println("PRINT - print a word (FIRST;LAST;nth)");
109          System.out.println("SWAP - exchange two elements (nth;mth)");
110          System.out.println("ADD - add a word to the list (FIRST;LAST)");
111          System.out.println("EXIT - terminate execution of the program");
112      }
113  
114      private static void interpreterSwapCommand(){
115          int position1 = commandReader.nextInt();
116          int position2 = commandReader.nextInt();
117          String temp = words[position1-1];
118          words[position1 -1] = words[position2-1];
119          words[position2 -1] = temp;
120      }
121  
122      private static void interpreterAddCommand() {
123          String position = commandReader.next();
124          if (position.equalsIgnoreCase("LAST")){
125              addlast();
126          } else if (position.equalsIgnoreCase("FIRST")){
127              addFirst();
128          } else {
129              System.out.println("### invalid operand for add command");
130          }
131          numberOfWords = numberOfWords + 1;
132      }
133  
134      private static void addlast() {
135          words[numberOfWords] = commandReader.next();
136      }
137  
138      private static void addFirst() {
139          for (int x = numberOfWords; x >0; x = x - 1){
140              words[x] = words [x - 1];
141          }
142          words[0] = commandReader.next();
143      }
144  }
145