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