1 2 3 package arraylist; 4 5 import java.io.File; 6 import java.io.FileNotFoundException; 7 import java.util.ArrayList; 8 import java.util.Scanner; 9 10 public class WordList { 11 // VARIABLES LOCAL TO THE CLASS, AND HENCE GLOBAL TO THE METHODS 12 private static final int LIMIT = 1000; 13 private static ArrayList<String> words = new ArrayList<>(LIMIT); 14 private static int numberOfNumbers = 0; 15 private static Scanner commandReader = new Scanner(System.in); 16 17 public static void main(String[] args) { 18 try { 19 // ESTABLISH THE ARRAY OF NUMBERS 20 readWordsFromFile(); 21 // CHECK THE DATA 22 // System.out.println("nThe original list of numbers ..."); 23 // displayNumbers(); 24 // ENTER THE INTERPRETER 25 interpreter(); 26 } catch (FileNotFoundException ex) { 27 System.out.println("The file was not found. Please think again."); 28 System.exit(-1); 29 } 30 } 31 32 // Assuming that the data file will be found in the public_html/data 33 // subdirectory of the user’s home directory. 34 private static Scanner establishScanner(String inputFileName) throws FileNotFoundException { 35 String separator = File.separator; 36 String homeDirectory = System.getProperty("user.home"); 37 String path = homeDirectory + separator + "public_html" + separator + "data" + separator; 38 String fullFileName = path + inputFileName; 39 return new Scanner(new File(fullFileName)); 40 } 41 42 private static void readWordsFromFile() throws FileNotFoundException { 43 Scanner scanner = establishScanner("WordSet.txt"); 44 while (scanner.hasNext()) { 45 String word = scanner.next(); 46 words.add(word); 47 } 48 } 49 50 private static void displayNumbers() { 51 //for (int x = 0; x < numberOfWords; x = x + 1) { 52 // System.out.println(words[x]); 53 // } 54 for (String w : words){ 55 System.out.println(w); 56 } 57 } 58 59 private static void interpreter() { 60 System.out.print(">>> "); 61 String command = commandReader.next(); 62 if (command.equalsIgnoreCase("DISPLAY")) { 63 interpreterDisplayCommand(); 64 } else if (command.equalsIgnoreCase("PRINT")) { 65 interpretPrintCommand(); 66 } else if (command.equalsIgnoreCase("SWAP")) { 67 interpretSwapCommand(); 68 } else if (command.equalsIgnoreCase("ADD")) { 69 interpretAddCommand(); 70 } else if (command.equalsIgnoreCase("HELP")) { 71 interpretHelpCommand(); 72 } else if (command.equalsIgnoreCase("EXIT")) { 73 System.exit(0); 74 } else { 75 System.out.println("### Unrecognizable command: " + command); 76 } 77 interpreter(); 78 } 79 80 private static void interpreterDisplayCommand() { 81 displayNumbers(); 82 } 83 84 private static void interpretPrintCommand() { 85 String operand = commandReader.next(); 86 if (operand.equalsIgnoreCase("FIRST")) { 87 System.out.println(words.get(0)); 88 } else if (operand.equalsIgnoreCase("LAST")) { 89 System.out.println(words.get(words.size()-1)); 90 } else { 91 int index = Integer.valueOf(operand); 92 System.out.println(words.get(index - 1)); 93 } 94 } 95 96 private static void interpretHelpCommand() { 97 System.out.println("HELP - display a menu of commands"); 98 System.out.println("DISPLAY - display the list of words"); 99 System.out.println("PRINT - print a word (FIRST;LAST;nth)"); 100 System.out.println("SWAP - exchange two elements (nth;mth)"); 101 System.out.println("ADD - add a number to the list (FIRST;LAST)"); 102 System.out.println("EXIT - terminate execution of the program"); 103 } 104 105 private static void interpretSwapCommand() { 106 int position1 = commandReader.nextInt(); 107 int position2 = commandReader.nextInt(); 108 String temp = words.get(position1 - 1); 109 words.set ((position1 - 1), words.get(position2 - 1)); 110 words.set(position2 - 1, temp); 111 } 112 113 private static void interpretAddCommand() { 114 String position = commandReader.next(); 115 if (position.equalsIgnoreCase("LAST")) { 116 addLast(); 117 } else if (position.equalsIgnoreCase("FIRST")) { 118 addFirst(); 119 } else { 120 System.out.println("### invalid operand for add command"); 121 } 122 numberOfNumbers = numberOfNumbers + 1; 123 } 124 125 private static void addLast() { 126 words.add(words.size(), commandReader.next()); 127 } 128 129 private static void addFirst() { 130 for (int x = numberOfNumbers; x > 0; x = x - 1) { 131 words.add(0,commandReader.next()); 132 // words[x] = words[x-1]; 133 } 134 } 135 }