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