1 2 /* 3 * Program featuring an array to store and interactively manipulate a list of numbers. 4 */ 5 package arrayplay; 6 7 import java.io.File; 8 import java.io.FileNotFoundException; 9 import java.util.Scanner; 10 11 public class WordList { 12 // VARIABLES LOCAL TO THE CLASS, AND HENCE GLOBAL TO THE METHODS 13 private static final int LIMIT = 1000; 14 private static String[] words = new String[LIMIT]; 15 private static int numberOfWords = 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 while (scanner.hasNext()) { 47 //Get rid of int the reads as a string 48 words[numberOfWords] = scanner.next(); 49 numberOfWords = numberOfWords + 1; 50 } 51 } 52 53 private static void displayNumbers() { 54 for (int x = 0; x < numberOfWords; x = x + 1) { 55 System.out.println(words[x]); 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[0]); 88 } else if (operand.equalsIgnoreCase("LAST")) { 89 System.out.println(words[numberOfWords - 1]); 90 } else { 91 int index = Integer.valueOf(operand); 92 System.out.println(words[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 numbers"); 99 System.out.println("PRINT - print a number (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 // 109 String temp = words[position1 - 1]; 110 words[position1 - 1] = words[position2 - 1]; 111 words[position2 - 1] = temp; 112 } 113 114 private static void interpretAddCommand() { 115 String position = commandReader.next(); 116 if (position.equalsIgnoreCase("LAST")) { 117 addLast(); 118 } else if (position.equalsIgnoreCase("FIRST")) { 119 addFirst(); 120 } else { 121 System.out.println("### invalid operand for add command"); 122 } 123 numberOfWords = numberOfWords + 1; 124 } 125 //adding what the command reader sees to the end of the last position array 126 private static void addLast() { 127 // numberOfWords is a counter 128 words[numberOfWords] = commandReader.next(); 129 } 130 131 private static void addFirst() { 132 for (int x = numberOfWords; x > 0; x = x - 1) { 133 words[x] = words[x - 1]; 134 } 135 words[0] = commandReader.next(); 136 } 137 } 138 139 140 141 142