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