Results 1 to 7 of 7
  1. #1
    Cylab is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default Stack can`t pop off (see the code). Java with problem?

    Dear all.
    Thanks for you kind and generous supporting.
    From the String s="<hell <world </world </hell";
    I wanted push the elements "<hello, <world" into stack, and pop off if current element equal to peek element in stack.

    Question: Even stack.peek().equals(CURRENT ELEMENT), why can not pop off, but occurs error??
    Please give me your suggestion. PLEASE!!!!

    Java Code:
    import java.util.*;
    
    public class GomiTest{
    	
           public static void main(String[] args){
    		Stack<String> stack = new Stack<String>();
    		String s="<hell <world </world </hell";
    		String temp[]=null;
    		String jin;
    		temp=s.split(" ");
    		
    		for(int i=0;i<temp.length;i++){
    			if(temp[i].startsWith("</")){
    				jin=temp[i].replace("/","");
    				//System.out.println("jin "+jin);
    				if(!stack.empty()&&stack.peek().equals(jin)){
    		          System.out.println("pop "+stack.pop());
    				}else{
    					System.out.println("error");
    				}
    			//	System.out.println("pushed "+temp[i].replace("</",""));
    			}else if(temp[i].startsWith("<")){
    				//jin=temp[i].replace("<","");
    				System.out.println("pushed "+temp[i]);
    			}else{
    				System.out.println("other "+temp[i]);
    			}
    		}
    	}
    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default

    Hi,

    I think your stack is always empty. Did you forget to push your data into the stack?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Cylab View Post
    Dear all.
    Thanks for you kind and generous supporting.
    From the String s="<hell <world </world </hell";
    I wanted push the elements "<hello, <world" into stack, and pop off if current element equal to peek element in stack.
    So now you removed the closing brackets '>' to avoid using a proper XML parser?

    kind regards,

    Jos

  4. #4
    Cylab is offline Member
    Join Date
    Jun 2010
    Posts
    29
    Rep Power
    0

    Default

    Dear all

    I think both of your right.
    Actually this is part of my assignment with strange requriements that I have been trying to for nearly a month.
    I hope there is sth that I could mark as solved of this thread.

    Thanks for your time and remembering .

    Ted

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Cylab View Post
    Dear all

    I think both of your right.
    Actually this is part of my assignment with strange requriements that I have been trying to for nearly a month.
    I hope there is sth that I could mark as solved of this thread.

    Thanks for your time and remembering .
    If you insist in doing it the stupid way, the String.indexOf(int ch, int fromIndex) method is your friend. Search for each '<' character from left to right, inspect the next character (if any) and if it is a '/' you have to pop something, if it isn't you have to push something. Check if the text to pop matches the text on the stack top.

    kind regards,

    Jos

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,319
    Rep Power
    25

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Jeez, first the OP refuses to use a proper solution by simply using an XML parser, next he shows the same problem but removes the right angular brackets '>' to rule out the proper solution and next s/he crossposts the same question. I'm out of here, thanks for informing us.

    kind regards,

    Jos

Similar Threads

  1. Stack problem
    By Clown in forum New To Java
    Replies: 9
    Last Post: 03-20-2010, 12:03 PM
  2. recursion and call stack problem
    By OptimusPrime in forum New To Java
    Replies: 4
    Last Post: 12-26-2009, 09:49 PM
  3. Stack problem..pls help
    By Mika in forum New To Java
    Replies: 1
    Last Post: 02-16-2009, 08:10 AM
  4. Problem in Calculator implementation using Stack
    By realahmed8 in forum New To Java
    Replies: 1
    Last Post: 12-19-2008, 11:58 PM
  5. error stack when I try to execute my code
    By paty in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 08:32 PM

Posting Permissions

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