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