1 /* 2 * Program featuring an array to store and interactively manipulate a list of numbers. 3 */ 4 5 6 package arrayPlay; 7 8 import java.io.File; 9 import java.io.FileNotFoundException; 10 import java.util.Scanner; 11 12 public class WordList { 13 //Variables local to the class, and hence global to the methods 14 private static final int LIMIT= 1000; 15 private static String[] Words = new String[LIMIT]; 16 private static int numberOfWords = 0; 17 private static Scanner commandReader= new Scanner(System.in); 18 19 public static void main(String[]args) { 20 try { 21 22 //Establish the array of numbers 23 readWords(); 24 //Check the data 25 System.out.println("\nThe original list of numbers..."); 26 displayNumbers(); 27 //Enter the interpreter 28 interpreter(); 29 } catch (FileNotFoundException ex) { 30 System.out.println("The file was not found. Please think again."); 31 System.exit(-1); 32 33 } 34 } 35 private static void readWords() throws FileNotFoundException { 36 Scanner scanner = establishScanner("WordSet.txt"); 37 while (scanner.hasNext()) { 38 Words[numberOfWords] = scanner.next(); 39 numberOfWords = numberOfWords + 1; 40 } 41 } 42 private static Scanner establishScanner (String fn) throws FileNotFoundException { 43 String separator = File.separator; 44 String homeDirectory = System.getProperty("user.home"); 45 String path = homeDirectory + separator + "public_html" + separator + "data" + separator; 46 String fullFileName = path + fn; 47 return new Scanner(new File(fullFileName)); 48 } 49 private static void interpreter () { 50 System.out.print(">>>"); 51 String command = commandReader.next(); 52 if (command.equalsIgnoreCase("DISPLAY")) { 53 interpreterDisplayCommand(); 54 } else if (command.equalsIgnoreCase("PRINT")) { 55 interpretPrintCommand(); 56 } else if (command.equalsIgnoreCase("SWAP")) { 57 interpretSwapCommand(); 58 } else if (command.equalsIgnoreCase("ADD")) { 59 interpretAddCommand(); 60 } else if (command.equalsIgnoreCase("HELP")) { 61 interpretHelpCommand(); 62 } else if (command.equalsIgnoreCase("EXIT")) { 63 System.exit(0); 64 } else { 65 System.out.println("### Unrecognizable command:" + command); 66 } 67 interpreter(); 68 } 69 70 private static void interpreterDisplayCommand() { 71 displayNumbers(); 72 } 73 74 private static void displayNumbers() { 75 for (int x = 0; x< numberOfWords; x=x+1){ 76 System.out.println(Words[x]); 77 } 78 } 79 80 private static void interpretPrintCommand() { 81 String operand= commandReader.next(); 82 if (operand.equalsIgnoreCase("FIRST")) { 83 System.out.println(Words[0]); 84 }else if (operand.equalsIgnoreCase("LAST")) { 85 System.out.println(Words[numberOfWords - 1]); 86 }else { 87 int index= Integer.valueOf(operand); 88 System.out.println(Words[index-1]); 89 } 90 } 91 private static void interpretSwapCommand() { 92 int position1= commandReader.nextInt(); 93 int position2= commandReader.nextInt(); 94 String temp= Words[position1-1]; 95 Words[position1-1]= Words[position2-1]; 96 Words[position2-1]=temp; 97 } 98 private static void interpretHelpCommand() { 99 System.out.println("HELP - display a menu of commands"); 100 System.out.println("DISPLAY - display the list of numbers"); 101 System.out.println("PRINT- print a number(FIRST; LAST; nth) "); 102 System.out.println("ADD - add a number to the list(FIRST; LAST)"); 103 System.out.println("EXIT - terminate execution of the program"); 104 } 105 private static void interpretAddCommand() { 106 String position= commandReader.next(); 107 if (position.equalsIgnoreCase("LAST")) { 108 addLast(); 109 }else if (position.equalsIgnoreCase("FIRST")) { 110 addFIRST(); 111 }else { 112 System.out.println("### invalid operand for add command"); 113 } 114 numberOfWords = numberOfWords +1; 115 } 116 private static void addLast() { 117 Words[numberOfWords]= commandReader.next(); 118 } 119 private static void addFIRST(){ 120 for (int x = numberOfWords; x>0; x=x-1){ 121 Words[x]= Words[x-1]; 122 } 123 Words[0]= commandReader.next(); 124 } 125 126 } 127 128 129 130 131 132