TheWordList2.java
1    package arraylistplay;
2    
3    /* 
4     * Program featuring an arraylist to store and interactively manipulate a list of words. 
5     */
6    import java.io.File;
7    import java.io.FileNotFoundException;
8    import java.util.ArrayList;
9    import java.util.Collections;
10   import java.util.Scanner;
11   
12   public class TheWordList2 {
13       // VARIABLES LOCAL TO THE CLASS, AND HENCE GLOBAL TO THE METHODS
14       //got rid of INDEX
15       private static ArrayList<String> words = new ArrayList<>(); //added ArrayList words
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()); //edited words[numberOfWords] = scanner.next();
47           }
48       }
49   
50       private static void displayNumbers() {
51           for (int x = 0; x < words.size(); x = x + 1) {
52               System.out.println(words.get(x)); //edited System.out.println(words[x]);
53           }
54       }
55   
56       private static void interpreter() {
57           System.out.print(">>> ");
58           String command = commandReader.next();
59           if (command.equalsIgnoreCase("DISPLAY")) {
60               interpreterDisplayCommand();
61           } else if (command.equalsIgnoreCase("PRINT")) {
62               interpretPrintCommand();
63           } else if (command.equalsIgnoreCase("SWAP")) {
64               interpretSwapCommand();
65           } else if (command.equalsIgnoreCase("ADD")) {
66               interpretAddCommand();
67           } else if (command.equalsIgnoreCase("HELP")) {
68               interpretHelpCommand();
69           } else if (command.equalsIgnoreCase("EXIT")) {
70               System.exit(0);
71           } else {
72               System.out.println("### Unrecognizable command: " + command);
73           }
74           interpreter();
75       }
76   
77       private static void interpreterDisplayCommand() {
78           displayNumbers();
79       }
80   
81       private static void interpretPrintCommand() {
82           String operand = commandReader.next();
83           if (operand.equalsIgnoreCase("FIRST")) {
84               System.out.println(words.get(0));
85           } else if (operand.equalsIgnoreCase("LAST")) {
86               System.out.println(words.get(words.size() - 1));
87           } else {
88               int index = Integer.valueOf(operand); //looks like this might need to be changed
89               System.out.println(words.get(index)); //this was edited System.out.println(words[index - 1]);
90           }
91       }
92   
93       private static void interpretHelpCommand() { //should be okay
94           System.out.println("HELP - display a menu of commands");
95           System.out.println("DISPLAY - display the list of words");
96           System.out.println("PRINT - print a word (FIRST;LAST;nth)");
97           System.out.println("SWAP - exchange two elements (nth;mth)");
98           System.out.println("ADD - add a word 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          Collections.swap(words, position1, position2); //this was edited to swap position1 with position2
106          //String temp = words[position1 - 1];
107          //        words[position1 - 1] = words[position2 - 1];
108          //        words[position2 - 1] = temp;
109  
110      }
111  
112      private static void interpretAddCommand() {
113          String position = commandReader.next();
114          if (position.equalsIgnoreCase("LAST")) {
115              addLast();
116          } else if (position.equalsIgnoreCase("FIRST")) {
117              addFirst();
118          } else {
119              System.out.println("### invalid operand for add command");
120          }
121      }
122  
123      private static void addLast() {
124          words.add(commandReader.next());
125      } //idk
126  
127      private static void addFirst() {
128  
129          words.add(0, commandReader.next());
130      }
131  }
132