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