1 /* 2 program featuring an array to store and interactively manipulate a list of words. 3 */ 4 5 package arrayplay; 6 7 import java.io.File; 8 import java.io.FileNotFoundException; 9 import java.util.Scanner; 10 11 public class WordList { 12 // variables local to the class and thus global to the method 13 private static final int LIMIT = 1000; 14 private static String inputFileName = "WordSet.txt"; 15 private static String[] words = new String [LIMIT]; 16 private static int numberOfWords = 0; 17 private static Scanner commandReader = new Scanner(System.in); 18 19 public static void main(String[] args) { 20 try { 21 // establish the array of words 22 readWordsFromFile(); 23 // check the data 24 System.out.println("The original list of words ..."); 25 displayWords(); 26 // enter the interpreter 27 interpreter(); 28 } catch (FileNotFoundException ex) { 29 System.out.println("The file was not found. Please think again."); 30 System.exit(-1); 31 } 32 } 33 34 // assuming that the data file will be found in public_html/data 35 // subdirectory of the user's home directory. 36 private static Scanner establishScanner(String fn) throws FileNotFoundException { 37 String separator = File.separator; 38 String homeDirectory = System.getProperty("user.home"); 39 String path = homeDirectory + separator + "public_html" + separator + "data" + separator; 40 String fullFileName = path + fn; 41 return new Scanner(new File(fullFileName)); 42 } 43 44 private static void readWordsFromFile() throws FileNotFoundException { 45 // equate a scanner with the input file 46 Scanner scanner = establishScanner(inputFileName); 47 // read the words from the file into an over-sized array 48 String[] temp = new String[LIMIT]; 49 int index = 0; 50 while (scanner.hasNext()) { 51 String word = scanner.next(); 52 temp[index] = word; 53 index = index + 1; 54 } 55 numberOfWords = index; 56 // transfer the words into a perfectly sized array 57 for (int x = 0; x < numberOfWords; x = x+1) { 58 words[x] = temp[x]; 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 private static void interpreter() { 69 System.out.print(">>>"); 70 String command = commandReader.next(); 71 if (command.equalsIgnoreCase("DISPLAY")) { 72 interpreterDisplayCommand(); 73 } else if (command.equalsIgnoreCase("PRINT")) { 74 interpretPrintCommand(); 75 } else if (command.equalsIgnoreCase("SWAP")) { 76 interpretSwapCommand(); 77 } else if (command.equalsIgnoreCase("ADD")) { 78 interpretAddCommand(); 79 } else if (command.equalsIgnoreCase("HELP")) { 80 interpretHelpCommand(); 81 } else if (command.equalsIgnoreCase("EXIT")) { 82 System.exit(0); 83 } else { 84 System.out.println("### Unrecognizable command: " + command); 85 } 86 interpreter(); 87 } 88 89 private static void interpreterDisplayCommand() { 90 displayWords(); 91 } 92 93 private static void interpretPrintCommand() { 94 String operand = commandReader.next(); 95 if (operand.equalsIgnoreCase("FIRST")) { 96 System.out.println(words[0]); 97 } else if (operand.equalsIgnoreCase("LAST")) { 98 System.out.println(words[numberOfWords-1]); 99 } else { 100 int index = Integer.valueOf(operand); 101 System.out.println(words[index-1]); 102 } 103 } 104 105 private static void interpretHelpCommand() { 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 number (FIRST;LAST;nth)"); 109 System.out.println("SWAP - exchange two elements (nth;mth)"); 110 System.out.println("ADD - add a number 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 String temp = words[position1 - 1]; 118 words[position1 - 1] = words[position2 - 1]; 119 words[position2 - 1] = temp; 120 } 121 122 private static void interpretAddCommand() { 123 String position = commandReader.next(); 124 if (position.equalsIgnoreCase("LAST")) { 125 addLast(); 126 } else if (position.equalsIgnoreCase("FIRST")) { 127 addFirst(); 128 } else { 129 System.out.println("### invalid operand for add command"); 130 } 131 numberOfWords = numberOfWords + 1; 132 } 133 134 private static void addLast() { 135 words[numberOfWords] = commandReader.next(); 136 } 137 138 private static void addFirst() { 139 for (int x = numberOfWords; x > 0; x = x-1) { 140 words[x] = words[x -1]; 141 } 142 words[0] = commandReader.next(); 143 } 144 }