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