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