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