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