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 //Program that uses an array to store and manipulate a list of words 9 //FILE MUST BE CALLED WordSet.txt 10 11 public class WordList { 12 // VARIABLES - Local to class : Global to methods 13 private static ArrayList<String> words = new ArrayList<>(); 14 private static int numberOfWords = 0; 15 private static Scanner commandReader = new Scanner(System.in); 16 17 public static void main(String[] args) { 18 try { 19 // Establish array of strings 20 readNumbers(); 21 // CHECK THE DATA 22 // System.out.println("nOriginal list of words ..."); 23 // displayNumbers(); 24 // Enter 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 fn) 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 + fn; 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")) { 60 interpreterDisplayCommand(); 61 } else if (command.equalsIgnoreCase("PRINT")) { 62 interpretPrintCommand(); 63 } else if (command.equalsIgnoreCase("SWAP")) { 64 interpretSwapCommand(); 65 } else if (command.equalsIgnoreCase("ADD")) { 66 interpretAddCommand(); 67 } else if (command.equalsIgnoreCase("HELP")) { 68 interpretHelpCommand(); 69 } else if (command.equalsIgnoreCase("EXIT")) { 70 System.exit(0); 71 } else { 72 System.out.println("### Unrecognizable command: " + command); 73 } 74 interpreter(); 75 } 76 77 private static void interpreterDisplayCommand() { 78 displayNumbers(); 79 } 80 81 private static void interpretPrintCommand() { 82 String operand = commandReader.next(); 83 if (operand.equalsIgnoreCase("FIRST")) { 84 System.out.println(words.get(0)); 85 } else if (operand.equalsIgnoreCase("LAST")) { 86 System.out.println(words.get(numberOfWords - 1)); 87 } else { 88 int index = Integer.valueOf(operand); 89 System.out.println(words.get(index - 1)); 90 } 91 } 92 93 private static void interpretHelpCommand() { 94 System.out.println("HELP - display a menu of commands"); 95 System.out.println("DISPLAY - display the list of numbers"); 96 System.out.println("PRINT - print a number (FIRST;LAST;nth)"); 97 System.out.println("SWAP - exchange two elements (nth;mth)"); 98 System.out.println("ADD - add a number to the list (FIRST;LAST)"); 99 System.out.println("EXIT - terminate execution of the program"); 100 } 101 102 private static void interpretSwapCommand() { 103 int position1 = commandReader.nextInt(); 104 int position2 = commandReader.nextInt(); 105 String temp = words.get(position1 - 1); 106 words.set((position1 - 1), words.get(position2 - 1)); 107 words.set((position2 - 1), temp); 108 } 109 110 private static void interpretAddCommand() { 111 String position = commandReader.next(); 112 if (position.equalsIgnoreCase("LAST")) { 113 addLast(); 114 } else if (position.equalsIgnoreCase("FIRST")) { 115 addFirst(); 116 } else { 117 System.out.println("### invalid operand for add command"); 118 } 119 numberOfWords = numberOfWords + 1; 120 } 121 122 private static void addLast() { 123 words.add( commandReader.next() ); 124 } 125 126 private static void addFirst() { 127 //for (int x = numberOfWords; x > 0; x = x - 1) { 128 // words.add(x, words.get(x-1)); 129 //} 130 words.add(0, commandReader.next()); 131 } 132 } 133 134