Hi all
The program i'm trying to write is meant to take a series of HTML codes as input and check to see if the all the tags in that code are matched or not (it should basically check all the opening tags and see if they have been closed) . So it should use a stack to do this. What I have in mind is that each opening tag is pushed on the stack; each closing tag is attempted to be matched against the top of the stack. Its output is a 'yes' if the tags are all matched and a 'no' otherwise.
...... However i'm having a bit of problem to do that!
I'd be happy for any clues on how to improve
public boolean TagCheck(String code)
Stack s=new Stack();
int i=0;
while ( i < code.length())
{
if (code .charAt(0) != '/' ) //Test tag: closing or opening
s.push(code);
else
if ( s.isEmpty() || !code.substring(1).equals(s.top()) )
throw new IllegalStateException("Tag mismatch");
else
s.pop();
}
if (!is.isEmpty()) //Tags left unmatched
throw new IllegalStateException("Closing tag(s) missing");
}
I don't even know if the way i've approached it is right or not.
My problem is with the strings and if the program should read 'all' characters or not.......i'm mixed up!