Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: LinkedStack - need help fixing code; should be quick fix

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default LinkedStack - need help fixing code; should be quick fix

    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: LinkedStack - need help fixing code; should be quick fix

    Please explain what "aren't working properly" means.
    Is there somethings wrong with the output that you posted? Can you explain what the output should be?

    How do you test the code? What you posted has many compiler errors because of missing classes.
    Last edited by Norm; August 28th, 2012 at 07:14 AM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Quick Code analysis please
    By svo in forum Java Theory & Questions
    Replies: 9
    Last Post: April 7th, 2012, 12:34 AM
  2. help fixing the code of gui
    By aperson in forum AWT / Java Swing
    Replies: 1
    Last Post: January 21st, 2012, 07:20 AM
  3. Help fixing my first program
    By javadude in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 9th, 2012, 06:52 PM
  4. Need help fixing Binary Search Tree code
    By fistpunch in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 6th, 2010, 11:22 AM
  5. Just Need Some quick help with code
    By mulkman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2010, 11:11 AM