954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Java Stack ... String

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!

scream2ice
Light Poster
33 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

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.

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

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.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

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

scream2ice
Light Poster
33 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 
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. ) and then you have some code in between, then you close that tag (e.g. ). 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!)

scream2ice
Light Poster
33 posts since Mar 2008
Reputation Points: 10
Solved Threads: 0
 

What you can do here is, use regex to detect a pattern such as this "" when you encounter this you push in onto the stack. On the other hand when you encounter the pattern "" 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.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 
nikolaos
Newbie Poster
10 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

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.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You