I included my Driver class, prompt, input, and output: my pop/peek commands aren't working properly - please help!
DRIVER:
//Lab 9 //Ashmi Patel /* Implement and test a LinkedStack. Activities: 1. Complete the implementation of the LinkedStack example provided in your text and in class. Preserve the packaging of the source code in the javafoundations and javafoundations.exceptions packages. That is, the Stack, LinkedStack and LinearNode classes should be located in the javafoundations package, and the required exception classes should be located in the javafoundations.exceptions package. 2. Create a Driver class that exercises (tests) the functionality of the LinkedStack you implemented. That is, create a LinkedStack<String> object in the Driver. Once created, open and read from an input file named “lab9.inp” which contains an unknown number of “commands”, one per line. For testing your lab, you can create your own input file, as per the command specifications below, but the input file I use to grade your lab will be of my creation (and not released to you). 3. The input file will contain an unknown number of lines, each with a command on it that mimics a stack operation. The details of the format, parameter (if any) and purpose of each command (as well as a small sample input file) are shown below. When the program runs and reads from the input file, each command will be processed in turn usually performing one or more operations on the stack of strings you have created in this lab. Handle gracefully (catch and continue processing the next line) any exceptions (including input, or stack-based). */ import javafoundations.*; import javafoundations.exceptions.EmptyCollectionException; import java.io.*; import java.util.Scanner; public class Driver{ public static void main(String[] values){ LinkedStack<String> stack = new LinkedStack<String>(); File input; Scanner scanner; Scanner parser; String command; String value; try { input = new File("lab9.inp"); scanner = new Scanner(input); } catch(FileNotFoundException e){ System.err.println("Input file 'lab9.inp' does not exist."); return; } while(scanner.hasNextLine()){ parser=new Scanner(scanner.nextLine()); command=parser.next(); if(parser.hasNext()){ value=parser.next(); } else{ value=""; } //push if(command.compareTo("push")==0){ stack.push(value); } //pop try{ if(command.compareTo("pop")==0){ System.out.println(stack.pop()); } } catch(EmptyCollectionException e){ System.err.println("Nothing to pop"); } //peek try{ if(command.compareTo("peek")==0){ System.out.println(stack.peek()); }} catch(EmptyCollectionException e){ System.err.println("Nothing to peek"); } //size if(command.compareTo("size")==0){ System.out.println("The size of the stack is currently " + stack.size()); } //isEmpty if(command.compareTo("isEmpty")==0){ if(stack.isEmpty()==true){ System.out.println("The stack is empty");} if(stack.isEmpty()==false){ System.out.println("The stack is not empty");}} //toString if(command.compareTo("toString")==0){ System.out.println(stack.toString());} } } }
INPUT:
size
isEmpty
push “Dr. Mike Martinovic”
push “Dr. Andrea Salgian”
push “Dr. Pete DePasquale”
push “Dr. Deborah Knox”
pop
size
isEmpty
pop
size
toString
push “Dr. Jikai Li”
push “Dr. Monihsa Pulimood”
peek
OUTPUT:
The size of the stack is currently 0
The stack is empty
Dr.
The size of the stack is currently 3
The stack is not empty
Dr.
The size of the stack is currently 2
<top of stack>
Dr.
Dr.
<bottom of stack>
Dr.