1 package arraylistplay; 2 3 import java.io.File; 4 import java.io.FileNotFoundException; 5 import java.util.ArrayList; 6 import java.util.Scanner; 7 8 public class WordList { 9 private static ArrayList<String> words = new ArrayList<>(); 10 private static int numberOfWords = 0; 11 private static Scanner commandReader = new Scanner(System.in); 12 public static void main(String[] args) { 13 try { 14 readWords(); 15 interpreter(); 16 } catch (FileNotFoundException ex) { 17 System.out.println("The file was not found. Please think again."); 18 System.exit(-1); 19 } 20 } 21 22 private static Scanner establishScanner(String fn) throws FileNotFoundException { 23 String separator = System.getProperty("file.separator"); 24 String homeDirectory = System.getProperty("user.home"); 25 String path = homeDirectory + separator + "public_html" + separator + "data" + separator; 26 String fullFileName = path + fn; 27 return new Scanner(new File(fullFileName)); 28 } 29 private static void readWords() throws FileNotFoundException { 30 Scanner scanner = establishScanner("WordSet.txt"); 31 while (scanner.hasNext()) { 32 words.add(numberOfWords, scanner.next()); 33 numberOfWords ++; 34 } 35 } 36 37 private static void displayWords() { 38 for (String word : words) { 39 System.out.println(word); 40 } 41 } 42 43 private static void interpreter() { 44 System.out.print(">>>"); 45 String command = commandReader.next(); 46 if (command.equalsIgnoreCase("DISPLAY")) { 47 interpreterDisplayCommand(); 48 } else if (command.equalsIgnoreCase("PRINT")) { 49 interpreterPrintCommand(); 50 } else if (command.equalsIgnoreCase("SWAP")) { 51 interpreterSwapCommand(); 52 } else if (command.equalsIgnoreCase("ADD")) { 53 interpreterAddCommand(); 54 } else if (command.equalsIgnoreCase("HELP")) { 55 interpreterHelpCommand(); 56 } else if (command.equalsIgnoreCase("EXIT")) { 57 System.exit(0); 58 } else { 59 System.out.println("### Unrecognizable command: " + command); 60 } 61 System.out.print("\n"); 62 interpreter(); 63 } 64 65 private static void interpreterDisplayCommand() { 66 displayWords(); 67 } 68 69 private static void interpreterPrintCommand() { 70 String operand = commandReader.next(); 71 if (operand.equalsIgnoreCase("FIRST")) { 72 System.out.println(words.get(0)); 73 } else if (operand.equalsIgnoreCase("LAST")) { 74 System.out.println(words.get(words.size() - 1)); 75 } else { 76 int index = Integer.valueOf(operand); 77 System.out.println(words.get(index - 1)); } 78 } 79 80 private static void interpreterSwapCommand() { 81 int position1 = commandReader.nextInt() - 1; 82 int position2 = commandReader.nextInt() - 1; 83 String temp = words.get(position1); 84 words.set(position1, words.get(position2)); 85 words.set(position2, temp); 86 } 87 88 private static void interpreterAddCommand() { 89 90 if (isNumeric()) { 91 int index = commandReader.nextInt() - 1; 92 String newWord = commandReader.next(); 93 if (index > numberOfWords) { 94 System.out.println("### invalid operand for add command!"); 95 } else { 96 words.add(index, newWord); 97 } 98 } else { 99 String position = commandReader.next(); 100 if (position.equalsIgnoreCase("FIRST")) {//displayWords(); 101 words.add(0, commandReader.next()); 102 } else if (position.equalsIgnoreCase("LAST")) { 103 words.add(words.size(), commandReader.next()); 104 } else { 105 System.out.println("### invalid operand for add command!"); 106 } 107 108 } 109 110 numberOfWords ++; 111 } 112 113 private static void interpreterHelpCommand() { 114 System.out.println("HELP - display menu of commands"); 115 System.out.println("DISPLAY - display the list of words"); 116 System.out.println("PRINT - print a word (FIRST; LAST; nth)"); 117 System.out.println("SWAP - exchange two elements (nth; mth)"); 118 System.out.println("ADD - add a word to the list"); 119 System.out.println("EXIT - terminate execution of the program"); 120 } 121 private static boolean isNumeric() { 122 return commandReader.hasNextInt(); 123 } 124 } 125 126