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