1 /* 2 * Program featuring an arraylist to store and interactively manipulate a list of string 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 final int LIMIT = 1000; 15 // new arraylist called words 16 private static ArrayList<String> words = new ArrayList<> (); 17 // private static int numberOfWords=0; 18 private static Scanner commandReader = new Scanner(System.in); 19 20 public static void main(String[] args){ 21 try{ 22 // ESTABLISH THE ARRAY OF NUMBERS 23 readWords(); 24 // CHECK THE DATA 25 // System.out.println("nThe original list of numbers..") 26 // displayNumber(): 27 // ENTER THE INTERPRETER 28 interpreter(); 29 } catch (FileNotFoundException ex) { 30 // If the file is not found, the system will catch it to tell the user 31 // to inform. then the system exit after 1 sec. 32 System.out.println("The file was not found. Please think again"); 33 System.exit(-1); 34 } 35 } 36 37 // Assuming the data file will be found in the public.html/data 38 // subdirectory of the user's home directory 39 private static Scanner establishScanner(String fn) throws FileNotFoundException{ 40 String separator = File.separator; 41 String homeDirectory = System.getProperty("user.home"); 42 // tell the system the direction of the file so its user/home/pubic_html/data 43 String path = homeDirectory + separator + "public_html" + separator + "CS1WorkSite" + separator + "arraylistplay" + separator; 44 String fullFileName = path + fn; 45 return new Scanner(new File(fullFileName)); 46 } 47 48 private static void readWords() throws FileNotFoundException { 49 // scanner will scan the new file in the directory given in the establishScanner methods 50 Scanner scanner = establishScanner("WordSet.txt"); 51 // scanner.hasNext() is a boolean that is true if there is another token in the input. 52 while (scanner.hasNext()) { 53 words.add(scanner.next()); 54 } 55 } 56 57 private static void displayWords(){ 58 // this will output everything added into the array 59 for (int x = 0; x < words.size(); x = x + 1 ){ 60 System.out.println(words.get(x)); 61 } 62 } 63 64 private static void interpreter() { 65 System.out.print(">>> "); 66 // commandReader(scanner) will return the next string from the system input 67 String command = commandReader.next(); 68 if (command.equalsIgnoreCase("DISPLAY")) { 69 interpreterDisplayCommand(); 70 } else if (command.equalsIgnoreCase("PRINT")){ 71 interpreterPrintCommmand(); 72 } else if (command.equalsIgnoreCase("SWAP")){ 73 interpreterSwapCommand(); 74 } else if (command.equalsIgnoreCase("ADD")) { 75 interpreterAddCommand(); 76 } else if (command.equalsIgnoreCase("HELP")) { 77 interpreterHelpCommand(); 78 } else if (command.equalsIgnoreCase("EXIT")) { 79 System.exit(0); 80 } else { 81 System.out.println("### Unregonizable Command: " + command); 82 } 83 interpreter(); 84 } 85 86 private static void interpreterDisplayCommand() { 87 // Can't we just write this before since it's only one command???? 88 displayWords(); 89 } 90 91 private static void interpreterPrintCommmand() { 92 String operand = commandReader.next(); 93 if (operand.equalsIgnoreCase("First")){ 94 System.out.println(words.get(0)); 95 } else if (operand.equalsIgnoreCase("LAST")){ 96 System.out.println(words.get(words.size()-1)); 97 } else { 98 // "Integer.valueOf(operand)" convert the string operand to an integer. Super useful 99 int index = Integer.valueOf(operand); 100 System.out.println(words.get(index-1)); 101 } 102 } 103 104 private static void interpreterHelpCommand() { 105 System.out.println("HELP - display a menu of commands"); 106 System.out.println("DISPLAY - display the list of words"); 107 System.out.println("PRINT - print a word (FIRST;LAST;nth)"); 108 System.out.println("SWAP - exchange two elements (nth;mth)"); 109 System.out.println("ADD - add a word to the list (FIRST;LAST)"); 110 System.out.println("EXIT - terminate execution of the program"); 111 } 112 113 private static void interpreterSwapCommand(){ 114 int position1 = commandReader.nextInt(); 115 int position2 = commandReader.nextInt(); 116 String temp = words.get(position1-1); 117 words.set(position1-1,words.get(position2-1)); 118 words.set(position2-1,temp); 119 } 120 121 private static void interpreterAddCommand() { 122 String position = commandReader.next(); 123 if (position.equalsIgnoreCase("LAST")){ 124 addlast(); 125 } else if (position.equalsIgnoreCase("FIRST")){ 126 addFirst(); 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(commandReader.next()); 135 } 136 137 private static void addFirst() { 138 words.add(0,commandReader.next()); 139 } 140 } 141