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!

Recommended Answers

All 7 Replies

You need to first tell us what exactly are you expecting to be passed in the "code" parameter.
Give us an example of how a correct format of String inside "code" (one that would output "yes") and an incorrect format example (one that would output "no").

Also are you even sure whether this code compiles, from what I see there are syntax errors in there also.

Yes your approach here seems to be correct. From what I understand your program should be similar to a expression matching program - one that checks for the proper use of paranthesis, box brackets, angular brackets, braces in an expression. If you are not familliar with such a program I suggest you get to know what it is and try it out first. Once the matching part is done, moving over other parts of the file, in your case HTML file, would be trivial.
Also take a look at the Regular Expressions in Java since I feel you would be needing that.

Thanks for your help.
I have actually tried the expression mathing code...and it works perfectly...but this one seems to be a bit more challenging

this is the code I've got for the parentheses check:

import java.util.Stack;
import java.util.EmptyStackException;
import javax.swing.JOptionPane;

public class ParenChecker {

   private static final String OPEN = "([{";
  
   private static final String CLOSE = ")]}";

     public static boolean isBalanced(String expression) {
     Stack s = new Stack(); // Create an empty stack.
     boolean balanced = true;
     try {
       int index = 0;
       while (balanced && index < expression.length()) {
         char nextCh = expression.charAt(index);
         if (isOpen(nextCh)) {
           s.push(new Character(nextCh));
         }
         else if (isClose(nextCh)) {
           char topCh = ( (Character) s.pop()).charValue();
           balanced = OPEN.indexOf(topCh)
               == CLOSE.indexOf(nextCh);
         }
         index++;
       }
     }
     catch (EmptyStackException ex) {
       balanced = false;
     }
     return (balanced && s.empty());
   }

    private static boolean isOpen(char ch) {
     return OPEN.indexOf(ch) > -1;
   }

   private static boolean isClose(char ch) {
     return CLOSE.indexOf(ch) > -1;
   }

   public static void main(String args[]) {
       
     String expression = JOptionPane.showInputDialog(
         "Enter an expression containing parentheses");
     if (ParenChecker.isBalanced(expression)) {
       JOptionPane.showMessageDialog(null, expression
                                     + " is balanced");
     }
     else {
       JOptionPane.showMessageDialog(null, expression
                                     + " is not balanced");
     }
     System.exit(0);
   }
}

can you tell which parts of it would i have to change to get the new code (i mean the one for the html code ), plz?
thanks

You need to first tell us what exactly are you expecting to be passed in the "code" parameter.
Give us an example of how a correct format of String inside "code" (one that would output "yes") and an incorrect format example (one that would output "no").
Also are you even sure whether this code compiles, from what I see there are syntax errors in there also.

The actual HTML code is passed to the TagCheck method.
You know in html codes.... you start a tag (e.g. <head> ) and then you have some code in between, then you close that tag (e.g. </head> ). This code should basically check that any tag that has been opened 'does' have matching tag to close. If all the tags have been matched, then the program outputs 'yes the tags are matched'....and so on.

And no i'm not sure if this compiles...I just said this is what I have in mind (i don't know how to convert my ideea into java code!)

What you can do here is, use regex to detect a pattern such as this "<T>" when you encounter this you push in onto the stack. On the other hand when you encounter the pattern "</T>" you pop the topmost element from the stack and check whether the pattern T from the current tag matches T from the popped tag element.

Also since the pattern "T" should be a legal HTML tag, you should have a collection of all valid HTML tags and check the pattern "T" against it.

If you remove everything that is not between < > then you will be left with something like this.
<html><head><title></title></head><body><p></P> </body></html>. this is a string.
suppose s is a string.
s = <html> hold its position (1)and length(6)
if you find another tag eg <head> then s = <head> and position (7)length(6) and so on.
The first time you will find a closing tag eg </title> compare s with title
if they match then remove characters between 13 character and 28.
28-13 = 2*length + 1.
then start from the begining.
if they dont match return no.
if you have been left with null string return yes.

But isn't it better to treat the HTML tags such as HEAD, BODY, TABLE as one and not consider them on a character by character basis. Read my post above.

Be a part of the DaniWeb community

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