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