1 package arraylistplay; 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 ArrayList<String> Strings = new ArrayList<>(); 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 displaywords(); 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 Strings.add(scanner.next()); 42 } 43 } 44 45 private static void displaywords() { 46 for (int x = 0; x < Strings.size(); x = x + 1) { 47 System.out.println(Strings.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 displaywords(); 74 } 75 76 private static void interpretPrintCommand() { 77 String operand = commandReader.next(); 78 if (operand.equalsIgnoreCase("FIRST")) { 79 System.out.println(Strings.get(0)); 80 } else if (operand.equalsIgnoreCase("LAST")) { 81 System.out.println(Strings.get(Strings.size() - 1)); 82 } else { 83 int index = Integer.valueOf(operand); 84 System.out.println(Strings.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 = Strings.get(position1 - 1); 101 Strings.set(position1 - 1, Strings.get(position2 - 1)); 102 Strings.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 } 115 116 117 private static void addLast() { 118 Strings.add(commandReader.next()); 119 } 120 121 private static void addFirst() { 122 Strings.add(0,commandReader.next()); 123 } 124 } 125 126 127