1 /* 2 * Program featuring an array to store and interactively manipulate a list of words. 3 */ 4 package arraylistplay; 5 6 import java.io.File; 7 import java.io.FileNotFoundException; 8 import java.io.IOException; 9 import java.util.Scanner; 10 import java.util.ArrayList; 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 private static ArrayList <String> words = new ArrayList<>(LIMIT); 16 private static int numberOfWords = 0; 17 private static Scanner commandReader = new Scanner(System.in); 18 19 public static void main(String[] args) { 20 try { 21 // ESTABLISH THE ARRAY OF Words 22 readWords(); 23 // CHECK THE DATA 24 // System.out.println("\nThe original list of words ..."); 25 // displayWords(); 26 // ENTER THE INTERPRETER 27 interpreter(); 28 } catch (IOException ex) { 29 System.out.println("The file was not found. Please think again."); 30 System.exit(-1); 31 } 32 } 33 34 //Make Compatible with my windows computer 35 public static String pathBuilder(){ 36 String separator = System.getProperty("file.separator"); 37 String home = null; 38 39 //Make Program Compatible With Windows (I use windows :P ) 40 if(System.getProperty("os.name").startsWith("Windows")) { 41 home = System.getProperty("user.dir"); 42 } 43 else if(System.getProperty("os.name").startsWith("Windows 10")) { 44 home = System.getProperty("user.dir"); 45 } 46 else { 47 home = System.getProperty("user.home"); 48 } 49 String path = home + separator + "CS1Files" + separator + "data" + separator; 50 return path; 51 } 52 53 54 // Assuming that the data file will be found in the public_html/data 55 // subdirectory of the user’s home directory. 56 private static Scanner establishScanner(String fn, String path) throws FileNotFoundException { 57 String fullFileName = path + fn; 58 return new Scanner(new File(fullFileName)); 59 } 60 61 private static void readWords() throws FileNotFoundException { 62 Scanner scanner = establishScanner("WordSet.txt",pathBuilder()); 63 while( scanner.hasNext()) { 64 words.add(scanner.next()); 65 numberOfWords = numberOfWords + 1; 66 } 67 68 } 69 70 private static void displayWords() { 71 for (int x = 0; x < numberOfWords; x = x + 1) { 72 System.out.println(words.get(x)); 73 } 74 } 75 76 private static void interpreter(){ 77 System.out.print(">>> "); 78 String command = commandReader.next(); 79 if (command.equalsIgnoreCase("DISPLAY")) { 80 interpreterDisplayCommand(); 81 } else if (command.equalsIgnoreCase("PRINT")) { 82 interpretPrintCommand(); 83 } else if (command.equalsIgnoreCase("SWAP")) { 84 interpretSwapCommand(); 85 } else if (command.equalsIgnoreCase("ADD")) { 86 interpretAddCommand(); 87 } else if (command.equalsIgnoreCase("HELP")) { 88 interpretHelpCommand(); 89 } else if(command.equalsIgnoreCase("CLEAR")) { 90 interpretClearCommand(); 91 } else if(command.equalsIgnoreCase("LS")) { 92 interpretListCommand(pathBuilder()); 93 } else if (command.equalsIgnoreCase("EXIT")) { 94 System.exit(0); 95 } else { 96 System.out.println("### Unrecognizable command: " + command); 97 } 98 interpreter(); 99 } 100 101 private static void interpreterDisplayCommand() { 102 displayWords(); 103 } 104 105 private static void interpretPrintCommand() { 106 String operand = commandReader.next(); 107 if (operand.equalsIgnoreCase("FIRST")) { 108 System.out.println(words.get(0)); 109 } else if (operand.equalsIgnoreCase("LAST")) { 110 System.out.println(words.get(numberOfWords - 1)); 111 } else { 112 int index = Integer.valueOf(operand); 113 System.out.println(words.get(index - 1)); 114 } 115 } 116 117 private static void interpretHelpCommand() { 118 System.out.println("HELP - display a menu of commands"); 119 System.out.println("DISPLAY - display the list of words"); 120 System.out.println("PRINT - print a String (FIRST;LAST;nth)"); 121 System.out.println("SWAP - exchange two elements (nth;mth)"); 122 System.out.println("ADD - add a word to the list (FIRST;LAST)"); 123 System.out.println("CLEAR - clears the console"); 124 System.out.println("LS - lists file in data directory"); 125 System.out.println("EXIT - terminate execution of the program"); 126 } 127 128 private static void interpretSwapCommand() { 129 int position1 = commandReader.nextInt() -1; // subtraction of 1 so we won't have to use "exact" values as index input 130 int position2 = commandReader.nextInt() -1; 131 words.set(position1, words.set(position2 , words.get(position1))); // swapping is here 132 } 133 134 private static void interpretAddCommand() { 135 String position = commandReader.next(); 136 if (position.equalsIgnoreCase("LAST")) { 137 addLast(); 138 } else if (position.equalsIgnoreCase("FIRST")) { 139 addFirst(); 140 } else { 141 System.out.println("### invalid operand for add command"); 142 } 143 numberOfWords = numberOfWords + 1; 144 } 145 146 147 private static void interpretClearCommand(){ 148 int intelliJConsoleHistorySize = 300; 149 150 for (int i = 0; i < intelliJConsoleHistorySize;++i) { 151 System.out.println(); 152 153 } 154 } 155 156 private static void interpretListCommand(String path){ 157 File dataFolder = new File(path); 158 File[] listOfFiles = dataFolder.listFiles(); 159 for( File element: listOfFiles) { 160 System.out.println(element); 161 } 162 } 163 164 private static void addLast() { 165 words.add(commandReader.next()); 166 } 167 168 private static void addFirst() { 169 for (int x = numberOfWords; x > 0; x = x - 1) { 170 words.get(x - 1); 171 } 172 words.add(0, commandReader.next()); 173 } 174 }