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