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