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