1 /* 2 *Program featuring an arrayList to store and interactively manipulate a list of Words. 3 */ 4 5 package arraylistplay; 6 7 import javax.print.attribute.SetOfIntegerSyntax; 8 import java.io.File; 9 import java.io.FileNotFoundException; 10 import java.util.ArrayList; 11 import java.util.Scanner; 12 13 public class WordList { 14 15 //Variables local to the class, and hence global to the methods 16 private static ArrayList<String>Words= new ArrayList<>(); 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 displayWords(); 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.add(scanner.next()); 39 } 40 } 41 private static Scanner establishScanner (String fn) throws FileNotFoundException { 42 String separator = File.separator; 43 String homeDirectory = System.getProperty("user.home"); 44 String path = homeDirectory + separator + "public_html" + separator + "data" + separator; 45 String fullFileName = path + fn; 46 return new Scanner(new File(fullFileName)); 47 } 48 private static void interpreter () { 49 System.out.print(">>>"); 50 String command = commandReader.next(); 51 if (command.equalsIgnoreCase("DISPLAY")) { 52 interpreterDisplayCommand(); 53 } else if (command.equalsIgnoreCase("PRINT")) { 54 interpretPrintCommand(); 55 } else if (command.equalsIgnoreCase("SWAP")) { 56 interpretSwapCommand(); 57 } else if (command.equalsIgnoreCase("ADD")) { 58 interpretAddCommand(); 59 } else if (command.equalsIgnoreCase("HELP")) { 60 interpretHelpCommand(); 61 } else if (command.equalsIgnoreCase("EXIT")) { 62 System.exit(0); 63 } else { 64 System.out.println("### Unrecognizable command:" + command); 65 } 66 interpreter(); 67 } 68 69 private static void interpreterDisplayCommand() { 70 displayWords(); 71 } 72 73 private static void displayWords() { 74 for (int x = 0; x< Words.size(); x=x+1){ 75 System.out.println(Words.get(x)); 76 } 77 } 78 private static void interpretPrintCommand() { 79 String operand= commandReader.next(); 80 if (operand.equalsIgnoreCase("FIRST")) { 81 System.out.println(Words.get(0)); 82 }else if (operand.equalsIgnoreCase("LAST")) { 83 System.out.println(Words.get(Words.size()-1)); 84 }else { 85 int index= Integer.valueOf(operand); 86 System.out.println(Words.get(index-1)); 87 } 88 } 89 private static void interpretSwapCommand() { 90 int position1= commandReader.nextInt(); 91 int position2= commandReader.nextInt(); 92 String temp= Words.get(position1-1); 93 Words.set(position1-1, Words.get(position2-1)); 94 //Words[position1-1]= Words[position2-1];// changing this statement to set(position2-1, temp) 95 Words.set(position2-1, temp); 96 } 97 private static void interpretHelpCommand() { 98 System.out.println("HELP - display a menu of commands"); 99 System.out.println("DISPLAY - display the list of numbers"); 100 System.out.println("PRINT- print a number(FIRST; LAST; nth) "); 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 private static void interpretAddCommand() { 105 String position= commandReader.next(); 106 if (position.equalsIgnoreCase("LAST")) { 107 addLast(); 108 }else if (position.equalsIgnoreCase("FIRST")) { 109 addFIRST(); 110 }else { 111 System.out.println("### invalid operand for add command"); 112 } 113 114 } 115 private static void addLast() { 116 Words.add(commandReader.next()); 117 } 118 private static void addFIRST(){ 119 Words.add(0,commandReader.next()); 120 121 } 122 123 } 124 125 126