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!!!!

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++){
				//System.out.println("jin "+jin);
		          System.out.println("pop "+stack.pop());
			//	System.out.println("pushed "+temp[i].replace("</",""));
			}else if(temp[i].startsWith("<")){
				System.out.println("pushed "+temp[i]);
				System.out.println("other "+temp[i]);

Recommended Answers

All 4 Replies

Where do you push into the stack?
Exactly what "error"s do you get?

@JamesCherrill , Sorry sir, it was my mistake
Thanks for attention given

Why are you peeking at the stack in the first place? Think of it as a real stack: push and pop only. When your current token has a '/', you pop the stack. If the item you pop is not equal to the current token (modulo '/', of course) then you know you have an invalid string. For a student code assignment, you can just throw off an error there.
If you want more robust code, you would write more complex handling, which might involve pushing the popped item back on the stack, but for now, do it simple.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.