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 public class WordList { 9 10 private static final int LIMIT = 1000; 11 private static ArrayList<String> words = new ArrayList<>(LIMIT); 12 //private static String[] words = new String[LIMIT]; 13 private static int numberOfWords = 0; 14 private static Scanner commandReader = new Scanner(System.in); 15 16 public static void main(String [] args){ 17 try{ 18 readWords(); 19 interpreter(); 20 } catch (FileNotFoundException ex){ 21 System.out.println("The file was not found. Please think again."); 22 System.exit(-1); 23 } 24 } 25 26 private static Scanner establishScanner(String fn) throws FileNotFoundException{ 27 String separator = File.separator; 28 String homeDirectory = System.getProperty("user.home"); 29 String path = homeDirectory + separator + "public_html" + separator + "data" + separator; 30 String fullFileName = path + fn; 31 return new Scanner((new File(fullFileName))); 32 } 33 34 private static void readWords() throws FileNotFoundException{ 35 Scanner scanner = establishScanner("WordSet.txt"); 36 while(scanner.hasNext()){ 37 String word = scanner.next(); 38 words.add(word); 39 } 40 } 41 42 private static void displayWords(){ 43 for ( String word : words){ 44 System.out.println(word); 45 } 46 } 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 interpreterPrintCommand(); 55 }else if (command.equalsIgnoreCase("SWAP")){ 56 interpreterSwapCommand(); 57 }else if (command.equalsIgnoreCase("ADD")){ 58 interpreterAddCommand(); 59 }else if (command.equalsIgnoreCase("HELP")){ 60 interpreterHelpCommand(); 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 interpreterHelpCommand() { 70 System.out.println("HELP - display a menu of commands"); 71 System.out.println("DISPLAY - display the list of word"); 72 System.out.println("PRINT - print a word (FIRST;LAST;nth"); 73 System.out.println("SWAP - excahnge two elements (nth;mth)"); 74 System.out.println("ADD - add a word to the list (FIRST;LAST"); 75 System.out.println("EXIT - terminate execution of the program"); 76 } 77 78 private static void interpreterAddCommand() { 79 String position = commandReader.next(); 80 if (position.equalsIgnoreCase("LAST")){ 81 addLast(); 82 } else if (position.equalsIgnoreCase("FIRST")){ 83 addFirst(); 84 } else { 85 System.out.println("### invalid operand for add command"); 86 } 87 numberOfWords = numberOfWords + 1; 88 } 89 90 private static void addFirst() { 91 words.add(0, commandReader.next()); 92 //for (int x = numberOfWords; x > 0; x = x - 1){ 93 // words.set(x) = words.set(x - 1); 94 //} 95 //words.add(0) = commandReader.next(); 96 } 97 98 private static void addLast() { 99 String word = commandReader.next(); 100 words.add(word); 101 } 102 103 private static void interpreterSwapCommand() { 104 int position1 = commandReader.nextInt(); 105 int position2 = commandReader.nextInt(); 106 String temp = words.get(position1 - 1); 107 words.set(position1 - 1, words.get(position2-1)); 108 words.set(position2 - 1, temp); 109 //words[position1 - 1] = words[position2 - 1]; 110 // words[position2 - 1] = temp; 111 } 112 113 private static void interpreterPrintCommand() { 114 String operand = commandReader.next(); 115 if (operand.equalsIgnoreCase("FIRST")){ 116 System.out.println(words.get(0)); 117 } else if (operand.equalsIgnoreCase("LAST")){ 118 System.out.println(words.get(numberOfWords - 1)); 119 } else { 120 int index = Integer.valueOf(operand); 121 System.out.println(words.get(index - 1)); 122 } 123 } 124 125 private static void interpreterDisplayCommand() { 126 displayWords(); 127 } 128 129 130 } 131 132