Results 1 to 4 of 4
  1. #1
    jefrazie is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Question Stack implementation

    Hi I am trying to write a program about learning stacks. My req's are:

    Write a stack class with the methods:
    peek
    size
    isEmpty
    toString

    I also write a driver class to test the stack. The driver should create a stack of Strings then read in a series of strings from input. If the string starts with an @ symbol, then it is a command otherwise it is pushed into the stack. The commands are:
    @POP
    @PEEK
    @SIZE
    @DISPLAY
    @QUIT (end the program and exit loop)

    My problem is not with the stacks but with seperating the strings from the commands. Im not quite sure how to differentiate between the two. My thoughts are to tokenize it somehow but Im not sure how. Here is my code so far:

    import java.util.*;

    public interface StackADT<T>
    {
    public void push(T element);

    public T pop();

    public T peek();

    public int size();

    public boolean isEmpty();

    public String toString();
    }


    import java.util.*;

    public class Driver
    {
    public static void main(String[] args)
    {
    int quit = 0;
    String input;
    Stack stack = new Stack();

    System.out.println("Enter a name or one of the following commands:");
    System.out.println("@POP @PEEK @SIZE @DISPLAY @QUIT");

    do
    {
    System.out.println("Please enter a command: ");

    Scanner in = new Scanner(System.in);
    input = in.nextLine();

    if (input == "@POP")
    System.out.println("The value is: " + stack.pop());
    else
    if (input == "@PEEK")
    System.out.println("The value is: " + stack.peek());
    else
    if (input == "@SIZE")
    System.out.println("The value is: " + stack.size());
    else
    if (input == "@DISPLAY")
    System.out.println("The value is: " + stack.toString());
    else
    if (input == "@QUIT")
    quit = 1;

    else
    stack.push(input);
    }
    while (quit != 1);
    }
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You should use the equals method to compare strings instead of ==.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    before comparing using equals() use trim() to trim the space
    Ramya:cool:

  4. #4
    jefrazie is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Help on Stack Implementation
    By danver_2009 in forum New To Java
    Replies: 1
    Last Post: 02-16-2009, 09:12 AM
  2. Problem in Calculator implementation using Stack
    By realahmed8 in forum New To Java
    Replies: 1
    Last Post: 12-20-2008, 12:58 AM
  3. Need little Help In Calculater Implementation..
    By realahmed8 in forum New To Java
    Replies: 6
    Last Post: 12-18-2008, 02:39 AM
  4. Graph DPS and BFS implementation
    By hey in forum New To Java
    Replies: 1
    Last Post: 01-09-2008, 10:19 PM
  5. Help with recursive implementation
    By toby in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:57 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •