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 9 10 11 12 13 14 15 private static final int LIMIT = 1000; 16 private static String[] words = new String[LIMIT]; 17 private static int numberOfWords = 0; 18 private static Scanner commandReader = new Scanner(System.in); 19 20 21 public static void main(String[] args) { 22 try { 23 // ESTABLISH THE ARRAY OF NUMBERS 24 readNumbers(); 25 26 // CHECK THE DATA 27 System.out.println("nThe original list of numbers ..."); 28 displayNumbers(); 29 // ENTER THE INTERPRETER 30 interpreter(); 31 } catch (FileNotFoundException ex) { 32 System.out.println("The file was not found. Please think again."); 33 System.exit(-1); 34 } 35 } 36 // Assuming that the data file will be found in the public_html/data 37 // subdirectory of the user’s home directory. 38 private static Scanner establishScanner(String fn) throws FileNotFoundException { 39 String separator = File.separator; 40 String homeDirectory = System.getProperty("user.home"); 41 String path = homeDirectory + separator + "public_html" + separator + "data" + separator + "WordSet.txt"; 42 String fullFileName = path ; 43 System.out.println(fullFileName); 44 return new Scanner(new File(fullFileName)); 45 } 46 47 private static void readNumbers() throws FileNotFoundException { 48 Scanner scanner = establishScanner("NumberSet.txt"); 49 while (scanner.hasNext()) { 50 words[numberOfWords] = scanner.next(); 51 numberOfWords = numberOfWords + 1; 52 } 53 } 54 55 private static void displayNumbers() { 56 for (int x = 0; x < numberOfWords; x = x + 1) { 57 System.out.println(words[x]); 58 } 59 } 60 61 private static void interpreter() { 62 System.out.print(">>> "); 63 String command = commandReader.next(); 64 if (command.equalsIgnoreCase("DISPLAY")) { 65 interpreterDisplayCommand(); 66 } else if (command.equalsIgnoreCase("PRINT")) { 67 interpretPrintCommand(); 68 } else if (command.equalsIgnoreCase("SWAP")) { 69 interpretSwapCommand(); 70 } else if (command.equalsIgnoreCase("ADD")) { 71 interpretAddCommand(); 72 } else if (command.equalsIgnoreCase("HELP")) { 73 interpretHelpCommand(); 74 } else if (command.equalsIgnoreCase("EXIT")) { 75 System.exit(0); 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 interpretPrintCommand() { 87 String operand = commandReader.next(); 88 if (operand.equalsIgnoreCase("FIRST")) { 89 System.out.println(words[0]); 90 } else if (operand.equalsIgnoreCase("LAST")) { 91 System.out.println(words[numberOfWords - 1]); 92 } else { 93 int index = Integer.valueOf(operand); 94 System.out.println(words[index - 1]); 95 } 96 } 97 98 private static void interpretHelpCommand() { 99 System.out.println("HELP - display a menu of commands"); 100 System.out.println("DISPLAY - display the list of numbers"); 101 System.out.println("PRINT - print a number (FIRST;LAST;nth)"); 102 System.out.println("SWAP - exchange two elements (nth;mth)"); 103 System.out.println("ADD - add a number to the list (FIRST;LAST)"); 104 System.out.println("EXIT - terminate execution of the program"); 105 } 106 107 private static void interpretSwapCommand() { 108 int position1 = commandReader.nextInt(); 109 int position2 = commandReader.nextInt(); 110 String temp = words[position1 - 1]; 111 words[position1 - 1] = words[position2 - 1]; 112 words[position2 - 1] = temp; 113 } 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 addLast() { 129 words[numberOfWords] = String.valueOf(commandReader.next()); 130 } 131 132 private static void addFirst() { 133 for (int x = numberOfWords; x > 0; x = x - 1) { 134 words[x] = words[x - 1]; 135 } 136 words[0] = String.valueOf(commandReader.next()); 137 } 138 139 140 141 142 143 144 } 145