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 12 of 12

Thread: Need help with my POP method!!!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with my POP method!!!

    I have LinkedStack class, what I simply want to do is make a delimiter match alogorithm, I also have the working algorithm for it but the problem is implementing the algorithm . I'm just stuck in my POP method and which will fix just one line of algorithm currently showing incompatible types.

    I have a abstract DataElement class, A CharElement class extending DataElement and a LinkedStack class. My LinkedStacked class has all the push, pop and peek methods. I need help in making the pop method a char type.

    Below is my LinkedStack Class, just before the class ends, I have the pop method, so you can scroll all the way down and see the method.
    Here is what I had previously, this method below worked perfectly when I tried to create a stack, and then peek and pop and then push again; but not with the delimiter algorithm method in my tester.

    public char pop() throws StackException
        {
            if (stackTop == null)
                throw new StackException();
            stackTop= stackTop.link
        }


    public class LinkedStack {
     
            protected class StackNode extends CharElement
            {
     
               public DataElement info;
               public StackNode link;        
     
            public StackNode()
            {
                info = null;
                link = null;
            }
     
            public StackNode(DataElement item, StackNode pointer)
            {
                info = item;
                link = pointer;
            }
     
            public String toString()
            {
                return info.toString();
            }
     }
     
        private StackNode stackTop;
     
     
        public LinkedStack()
        {
            stackTop = null;
        }
     
        public void initializeStack()
        {
            stackTop = null;
        }
     
        public boolean isEmptyStack()
        {
            return (stackTop == null);
        }
     
        public boolean isFullStack()
        {
            return false;
        }
     
        public void push(DataElement newItem)
        {
            StackNode newTop;
            newTop  = new StackNode(newItem, stackTop );
            stackTop = newTop;
        }
     
        public DataElement peek() throws StackException
        {
            if (stackTop == null)
                throw new StackException();
            return stackTop.info;
        }
     
     
     
        public char pop() throws StackException   <-----------------------------------------------------------------------HERE IS THE POP METHOD, HELP ME CODE It, i need a return!!
        {
            if (stackTop == null)
                throw new StackException();
     
     
        }
     
       }

    here is my tester,
    there is still stuff to write but having an error on one line of the method 'i'sdelimitermatching', I have marked it with arrows!

    public class Tester {
        public static void main(String[] args) {
     
        LinkedStack stack1 = new LinkedStack();
        stack1.push(new CharElement('a'));
        stack1.push(new CharElement('b'));
        stack1.push(new CharElement('c'));
        stack1.push(new CharElement('d'));
        stack1.push(new CharElement('e'));
     
     
     
        System.out.println("Top of the Stack = " + stack1.peek());  
     
        while (!stack1.isEmptyStack())
        {
            System.out.println(stack1.peek());
     
            stack1.pop();
     
        }
     
        } 
     
        public boolean isDelimiterMatching(String inputExpr) {
     
     
            int stackSize = inputExpr.length();
            LinkedStack theStack = new LinkedStack();
            for (int j = 0; j < inputExpr.length(); j++) {
                char ch = inputExpr.charAt(j);
                switch (ch) {
                case '{':
                case '[':
                case '(':
                        String temp = Character.toString(ch);
                        theStack.pop();     <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Here is it, gotta fix it by fixing the pop method!
                        break;
                case '}':
                case ']':
                case ')':
                        if (!theStack.isEmptyStack()) {
                            char stackContent;
                           stackContent = theStack.pop();
                            if ((ch == '}' && stackContent!= '{') 
                                    || (ch == ']' && stackContent != '[')
                                    || (ch == ')' && stackContent != '(')){
                                System.out.println("Mismatch found: " + ch + " at " + j);
                                return false;
                            }
                        } else {
                            System.out.println("Mismatch found: " + ch + " at " + j);
                            return false;
                        }
                        break;
                default: break;
                }
            }
            if (!theStack.isEmptyStack()){
                System.out.println("Error: missing right delimiter");
                return false;
            }
            return true;
        }


    Well I have the major part of the code but the problem is the Pop method, I need it to cut the head of my stack, i mean remove the top and at the same time return me the top that it just took off so that I can store in the char variable in my "isDelimittermatching" method(Marked with arrows above).
    Please help me out write the pop method! Have been trying for ours now!


  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: Need help with my POP method!!!

    Comment: should the code save what is returned by the pop() method?

    Where can the pop() method get the data it is supposed to return? How can the pop() method remove the top of the stack so that the second node in the stack becomes the top of the stack?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my POP method!!!

    Quote Originally Posted by Norm View Post
    Comment: should the code save what is returned by the pop() method?

    Where can the pop() method get the data it is supposed to return? How can the pop() method remove the top of the stack so that the second node in the stack becomes the top of the stack?
    should the code save what is returned by the pop() method?
    Yes, it should save whatever its taking off, at least that is what I'm guessing because see on my tester below, I want to store whatever it is popping in the char variable, I'm sorry the arrow mark in my tester was in the wrong position. I've corrected it now. The method pop was first void and then I made it to char to work with the code below, but I need the pop method to pop and also return what it popped.
     char stackContent;
                           stackContent = theStack.pop();
    Where can the pop() method get the data it is supposed to return?
    from stackTop I guess, which is in LinkedStack class.
    How can the pop() method remove the top of the stack so that the second node in the stack becomes the top of the stack?
    In the void method that I have written at the very top of my post, it is kinda removing it, more like changing the reference. But If you could help in removing and also at the same time return whatever it is removing that would be great!

  4. #4
    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: Need help with my POP method!!!

    If you know where the data exists that pop() is supposed to return, how can pop() get that data?

    To remove the top/first of a linked list, get a pointer to the first node in the list from the top-of-the-list pointer and copy that node's connecting link to the top-of-the-list pointer.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my POP method!!!

    Quote Originally Posted by Norm View Post
    If you know where the data exists that pop() is supposed to return, how can pop() get that data?

    To remove the top/first of a linked list, get a pointer to the first node in the list from the top-of-the-list pointer and copy that node's connecting link to the top-of-the-list pointer.
    This what I did, it says incomaptible data types, the real questions is how do I convert? I do have a getCopy method in my char element class. So far thw wrong code is below,
     public char pop() throws StackException
        {
            if (stackTop == null)
                throw new StackException();
     
            char entry = this.stackTop.getCopy();
        }


    and then I could do stackTop = stackTop.link;
    and return entry. But how do I convert from DataElement to char. (CharElement extends from DataElement)

    --- Update ---

    I tried casting it, still error on return statement help me out, its due today! just one method and I'm done!
    public char pop() throws StackException
        {
            if (stackTop == null)
                throw new StackException();
     
                DataElement entry = this.stackTop.getCopy();
                stackTop = stackTop.link;
                return (new DataElement(entry));
        }

  6. #6
    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: Need help with my POP method!!!

    If pop() returns a char, then it needs to get a char from the element it removes from the stack.


    I don't understand why pop() is not the opposite of push() and why peek() is also different. They both work with DataElements.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my POP method!!!

    Here is my code, all the classes are in here, you can skip stackexception and strelement(does nothing - to be deleted) classes: https://compilr.com/xemkruz/linkedst...nkedStack.java

    How can i make pop opposite of push, its so different! i have 4 more hours to submit this! Just one method!

  8. #8
    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: Need help with my POP method!!!

    How can i make pop opposite of push,
    push() adds a DataElement object to the stack,
    peek() returns the DataElement that is at the top of the stack
    pop should return a DataElement object from the top of the stack like peek() does

    or push() and peek() should be changed so they work with a char like you want pop() to do.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my POP method!!!

    Quote Originally Posted by Norm View Post
    push() adds a DataElement object to the stack,
    peek() returns the DataElement that is at the top of the stack
    pop should return a DataElement object from the top of the stack like peek() does

    or push() and peek() should be changed so they work with a char like you want pop() to do.

    Aint I close? I jst need the return statement to work below, tried casting charElement into return entry since it is char type !, nope doesn't work! But the code makes sense?

    public char pop() throws StackException
    {
    if (stackTop == null)
    throw new StackException();

    DataElement entry = this.stackTop.getCopy();
    stackTop = stackTop.link;
    return (new CharElement(entry); <<<< Error!!
    }

  10. #10
    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: Need help with my POP method!!!

    I think the design is wrong.
    If push() puts a DataElement on the stack
    and peek() shows what's on the top of the stack as a DataElement
    then pop() should return a DataElement

    all three should work with the same data type.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Nov 2012
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with my POP method!!!

    Quote Originally Posted by Norm View Post
    I think the design is wrong.
    If push() puts a DataElement on the stack
    and peek() shows what's on the top of the stack as a DataElement
    then pop() should return a DataElement

    all three should work with the same data type.
    yeah, I can do that, but if I do that,
    char stackContent shows me error,

    char stackContent;
                           stackContent = theStack.pop();

    how can i put whatever is in pop which is dataelement into a char?

  12. #12
    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: Need help with my POP method!!!

    If the returned value is a DataElement, then the variable that is assigned what pop() returns must be defined as a DataElement.

    How does the code test the pop() method? Look at the variables used there.

    What is there a char? What is inside of the DataElement class? Does it have a method to access its contents?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Pop up that takes data from user
    By skashwin89 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 23rd, 2013, 02:17 PM
  2. java pop-up actions
    By Ravi Raj in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 6th, 2013, 12:44 AM
  3. confirmation pop up, help!
    By jamesy23 in forum Other Programming Languages
    Replies: 2
    Last Post: June 3rd, 2012, 07:58 AM
  4. [SOLVED] Jframe Pop outs when clicking JButton
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 22nd, 2012, 05:09 PM
  5. Pop-out box using a JFrame.
    By ShaunB in forum Java Theory & Questions
    Replies: 6
    Last Post: April 28th, 2011, 12:03 PM