/home/ssingh6/NetBeansProjects/CS1/src/arrayplay/WordList.java |
1
2
3
4
5
6 package arrayplay;
7
8 import java.io.File;
9 import java.io.FileNotFoundException;
10 import java.util.Scanner;
11
12
13
14 @author
15
16 public class WordList{
17
18
19 @param args
20
21 private static final int LIMIT = 1000;
22 private static String[] words = new String[LIMIT];
23 private static int numberOfWords = 0;
24 private static Scanner commandReader = new Scanner(System.in);
25
26
27 public static void main(String[] args) {
28 try {
29
30 readWords();
31
32
33
34
35 interpreter();
36 } catch (FileNotFoundException ex) {
37 System.out.println("The file was not found. Please think again.");
38 System.exit(-1);
39 }
40 }
41
42
43
44 private static Scanner establishScanner(String fn) throws FileNotFoundException {
45 String separator = System.getProperty("file.separator");
46 String homeDirectory = System.getProperty("user.home");
47 String path = homeDirectory + separator + "CS1Files" + separator + "data" + separator;
48 String fullFileName = path + fn;
49 return new Scanner(new File(fullFileName));
50 }
51 private static void readWords() throws FileNotFoundException {
52 Scanner scanner = establishScanner("WordSet.txt");
53 while (scanner.hasNext()) {
54 words[numberOfWords] = scanner.next();
55 numberOfWords ++;
56 }
57 }
58
59 private static void displayWords() {
60 for (int x = 0; x < numberOfWords; x ++) {
61 System.out.println(words[x]);
62 }
63 }
64
65 private static void interpreter() {
66 System.out.print(">>>");
67 String command = commandReader.next();
68 if (command.equalsIgnoreCase("DISPLAY")) {
69 interpreterDisplayCommand();
70 } else if (command.equalsIgnoreCase("PRINT")) {
71 interpreterPrintCommand();
72 } else if (command.equalsIgnoreCase("SWAP")) {
73 interpreterSwapCommand();
74 } else if (command.equalsIgnoreCase("ADD")) {
75 interpreterAddCommand();
76 } else if (command.equalsIgnoreCase("HELP")) {
77 interpreterHelpCommand();
78 } else if (command.equalsIgnoreCase("EXIT")) {
79 System.exit(0);
80 } else {
81 System.out.println("### Unrecognizable command: " + command);
82 }
83 System.out.print("\n");
84 interpreter();
85 }
86
87 private static void interpreterDisplayCommand() {
88 displayWords();
89 }
90
91 private static void interpreterPrintCommand() {
92 String operand = commandReader.next();
93 if (operand.equalsIgnoreCase("FIRST")) {
94 System.out.println(words[0]);
95 } else if (operand.equalsIgnoreCase("LAST")) {
96 System.out.println(words[numberOfWords-1]);
97 } else {
98 int index = Integer.valueOf(operand);
99 System.out.println(words[index-1]);
100 }
101 }
102
103 private static void interpreterSwapCommand() {
104 int position1 = commandReader.nextInt();
105 int position2 = commandReader.nextInt();
106 String temp = words[position1 - 1];
107 words[position1 - 1] = words[position2 - 1];
108 words[position2 - 1] = temp;
109 }
110
111 private static void interpreterAddCommand() {
112 String position = commandReader.next();
113 if (position.equalsIgnoreCase("LAST")) {
114 addLast();
115 } else if (position.equalsIgnoreCase("FIRST")) {
116 addFirst();
117 } else {
118 System.out.println("### Invalid operand for add command");
119 }
120 numberOfWords ++;
121 }
122
123 private static void interpreterHelpCommand() {
124 System.out.println("HELP - display menu of commands");
125 System.out.println("DISPLAY - display the list of words");
126 System.out.println("PRINT - print a word (FIRST; LAST; nth)");
127 System.out.println("SWAP - exchange two elements (nth; mth)");
128 System.out.println("ADD - add a word to the list (FIRST; LAST)");
129 System.out.println("EXIT - terminate execution of the program");
130 }
131
132 private static void addFirst() {
133 for (int x = numberOfWords; x > 0; x --) {
134 words[x] = words[x - 1];
135 }
136 words[0] = commandReader.next();
137 }
138
139 private static void addLast() {
140 words[numberOfWords] = commandReader.next();
141 }
142
143 }
144
145