1 /* 2 * Program featuring an array to store and interactively manipulate a list of numbers. 3 */ 4 package arraylistplay; 5 6 import java.io.File; 7 import java.io.FileNotFoundException; 8 import java.util.ArrayList; 9 import java.util.Scanner; 10 11 public class WordList { 12 // VARIABLES LOCAL TO THE CLASS, AND HENCE GLOBAL TO THE METHODS 13 14 private static ArrayList < String> numbers = new ArrayList <String> (); 15 private static int numberOfNumbers = 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 NUMBERS 21 readNumbers(); 22 // CHECK THE DATA 23 // System.out.println("nThe original list of numbers ..."); 24 // displayNumbers(); 25 // ENTER THE INTERPRETER 26 interpreter(); 27 } catch (FileNotFoundException ex) { 28 System.out.println("The file was not found. Please think again."); 29 System.exit(-1); 30 } 31 } 32 33 // Assuming that the data file will be found in the public_html/data 34 // subdirectory of the user’s home directory. 35 private static Scanner establishScanner(String fn) throws FileNotFoundException { 36 String separator = File.separator; 37 String homeDirectory = System.getProperty("user.home"); 38 // System.out.println(homeDirectory); 39 String path = homeDirectory + separator + "public_html" + separator + "data" + separator; 40 String fullFileName = path + fn; 41 return new Scanner(new File(fullFileName)); 42 } 43 44 private static void readNumbers() throws FileNotFoundException { 45 Scanner scanner = establishScanner("WordSet.txt"); 46 47 //checks to see if there is any thing else left to scan 48 while (scanner.hasNext()) { 49 50 // gives you the next value that the scanner sees 51 numbers.add(scanner.next()); 52 numberOfNumbers = numberOfNumbers + 1; 53 } 54 } 55 56 57 // Display everything in arraylist 58 private static void displayNumbers() { 59 //ForEach- the functions in the for each block will be applied to everything in the stream 60 numbers.stream().forEach( x -> System.out.println(x)); 61 62 } 63 64 private static void interpreter() { 65 System.out.print(">>> "); 66 String command = commandReader.next(); 67 if (command.equalsIgnoreCase("DISPLAY")) { 68 interpreterDisplayCommand(); 69 } else if (command.equalsIgnoreCase("PRINT")) { 70 interpretPrintCommand(); 71 } else if (command.equalsIgnoreCase("SWAP")) { 72 interpretSwapCommand(); 73 } else if (command.equalsIgnoreCase("ADD")) { 74 interpretAddCommand(); 75 } else if (command.equalsIgnoreCase("HELP")) { 76 interpretHelpCommand(); 77 } else if (command.equalsIgnoreCase("EXIT")) { 78 System.exit(0); 79 } else { 80 System.out.println("### Unrecognizable command: " + command); 81 } 82 interpreter(); 83 } 84 85 private static void interpreterDisplayCommand() { 86 displayNumbers(); 87 } 88 89 private static void interpretPrintCommand() { 90 String operand = commandReader.next(); 91 if (operand.equalsIgnoreCase("FIRST")) { 92 System.out.println (numbers.get(0)); 93 } else if (operand.equalsIgnoreCase("LAST")) { 94 System.out.println(numbers.get(numberOfNumbers - 1)); 95 } else { 96 int index = Integer.valueOf(operand); 97 System.out.println(numbers.get(index - 1)); 98 } 99 } 100 101 private static void interpretHelpCommand() { 102 System.out.println("HELP - display a menu of commands"); 103 System.out.println("DISPLAY - display the list of numbers"); 104 System.out.println("PRINT - print a number (FIRST;LAST;nth)"); 105 System.out.println("SWAP - exchange two elements (nth;mth)"); 106 System.out.println("ADD - add a number to the list (FIRST;LAST)"); 107 System.out.println("EXIT - terminate execution of the program"); 108 } 109 110 private static void interpretSwapCommand() { 111 int position1 = commandReader.nextInt(); 112 int position2 = commandReader.nextInt(); 113 //temporary 114 String temp = numbers.get(position1 - 1); 115 116 //we took out what was in position 1-1 and moved that to temp. We copied and now position 1 is empty so we copy 117 //what was in position 2 to position 1 118 119 numbers.add(position1-1, numbers.get(position2-1)); 120 numbers.remove(position1); 121 numbers.add(position2-1, temp); 122 numbers.remove (position2); 123 } 124 125 private static void interpretAddCommand() { 126 String position = commandReader.next(); 127 if (position.equalsIgnoreCase("LAST")) { 128 addLast(); 129 } else if (position.equalsIgnoreCase("FIRST")) { 130 addFirst(); 131 } else { 132 System.out.println("### invalid operand for add command"); 133 } 134 numberOfNumbers = numberOfNumbers + 1; 135 } 136 137 private static void addLast() { 138 //whenever we add something to an arraylist it gets added to the end of the arraylist. 139 numbers.add (commandReader.next()); 140 } 141 142 private static void addFirst() { 143 144 numbers.add(0, commandReader.next()); 145 146 147 148 } 149 } 150 151 152 153 154