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