The following text was written to the standard output stream when the WordListA program was executed from Netbeans.
/*
*Program featuring an array to store and interactively manipulate a list
*of words.
*/
package arraylistplay;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @authorblue
*/
public class WordList {
// VARIABLES LOCAL TO THE CLASS, AND HENCE GLOBAL TO THE METHODS
private static ArrayListwords = new ArrayList<>();
private static int numberOfWords = 0;
private static Scanner commandReader = new Scanner(System.in);
/**
* @param args
* @paramargsthecommandlinearguments
*/
public static void main(String[] args) {
try {
// ESTABLISH THE ARRAY OF WORDS
readWords();
// CHECK THE DATA
// ENTER THE INTERPRETER
interpreter();
} catch (FileNotFoundException ex) {
System.out.println("The file was not found. Please think again.");
System.exit(-1);
}
}
// Assuming that the data file will be found in the public_html/data
// subdirectory of the user’s home directory.
private static Scanner establishScanner(String fn) throws FileNotFoundException {
String separator = System.getProperty("file.separator");
String home = System.getProperty("user.home");
String path = home + separator + "CS1Files" + separator + "data" + separator;
String WordSet = path + fn;
return new Scanner(new File(WordSet));
}
private static void readWords() throws FileNotFoundException {
Scanner scanner = establishScanner("WordSet");
while (scanner.hasNext()) {
String word = scanner.next();
words.add(word);
}
}
private static void displayWords() {
for (String word : words) {
System.out.println(word);
}
}
private static void interpreter() {
System.out.print(">>> ");
String command = commandReader.next();
if (command.equalsIgnoreCase("DISPLAY")) {
interpreterDisplayCommand();
} else if (command.equalsIgnoreCase("PRINT")) {
interpretPrintCommand();
} else if (command.equalsIgnoreCase("SWAP")) {
interpretSwapCommand();
} else if (command.equalsIgnoreCase("ADD")) {
interpretAddCommand();
} else if (command.equalsIgnoreCase("HELP")) {
interpretHelpCommand();
} else if (command.equalsIgnoreCase("EXIT")) {
System.exit(0);
} else {
System.out.println("### Unrecognizable command: " + command);
}
interpreter();
}
private static void interpreterDisplayCommand() {
displayWords();
}
private static void interpretPrintCommand() {
String operand = commandReader.next();
if (operand.equalsIgnoreCase("FIRST")) {
System.out.println(words.get(0));
} else if (operand.equalsIgnoreCase("LAST")) {
System.out.println(words.get(words.size()-1));
} else {
int index = Integer.valueOf(operand);
System.out.println(words.get(index-1));
}
}
private static void interpretHelpCommand() {
System.out.println("HELP - display a menu of commands");
System.out.println("DISPLAY - display the list of numbers");
System.out.println("PRINT - print a number (FIRST;LAST;nth)");
System.out.println("SWAP - exchange two elements (nth;mth)");
System.out.println("ADD - add a number to the list (FIRST;LAST)");
System.out.println("EXIT - terminate execution of the program");
}
private static void interpretSwapCommand() {
int position1 = commandReader.nextInt();
int position2 = commandReader.nextInt();
//Set not get, change setup
String temp = words.get(position1 - 1);
words.set(position1 - 1, words.get(position2-1));
words.set(position2 - 1, temp);
}
private static void interpretAddCommand() {
String position = commandReader.next();
if (position.equalsIgnoreCase("LAST")) {
addLast();
} else if (position.equalsIgnoreCase("FIRST")) {
addFirst();
} else {
System.out.println("### invalid operand for add command");
}
numberOfWords = numberOfWords + 1;
}
private static void addLast() {
words.add(words.size(), commandReader.next());
}
private static void addFirst() {
words.add(0, commandReader.next());
}
}