Java Stack ... String

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2008
Posts: 31
Reputation: scream2ice is an unknown quantity at this point 
Solved Threads: 0
scream2ice scream2ice is offline Offline
Light Poster

Java Stack ... String

 
0
  #1
Feb 12th, 2009
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

  1. public boolean TagCheck(String code)
  2. Stack s=new Stack();
  3. int i=0;
  4. while ( i < code.length())
  5. {
  6. if (code .charAt(0) != '/' ) //Test tag: closing or opening
  7. s.push(code);
  8. else
  9. if ( s.isEmpty() || !code.substring(1).equals(s.top()) )
  10. throw new IllegalStateException("Tag mismatch");
  11. else
  12. s.pop();
  13. }
  14. if (!is.isEmpty()) //Tags left unmatched
  15. throw new IllegalStateException("Closing tag(s) missing");
  16.  
  17. }

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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Java Stack ... String

 
0
  #2
Feb 12th, 2009
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.
Last edited by stephen84s; Feb 12th, 2009 at 4:59 am.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Java Stack ... String

 
0
  #3
Feb 12th, 2009
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.
Last edited by verruckt24; Feb 12th, 2009 at 5:10 am.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 31
Reputation: scream2ice is an unknown quantity at this point 
Solved Threads: 0
scream2ice scream2ice is offline Offline
Light Poster

Re: Java Stack ... String

 
0
  #4
Feb 12th, 2009
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:

  1. import java.util.Stack;
  2. import java.util.EmptyStackException;
  3. import javax.swing.JOptionPane;
  4.  
  5. public class ParenChecker {
  6.  
  7. private static final String OPEN = "([{";
  8.  
  9. private static final String CLOSE = ")]}";
  10.  
  11. public static boolean isBalanced(String expression) {
  12. Stack s = new Stack(); // Create an empty stack.
  13. boolean balanced = true;
  14. try {
  15. int index = 0;
  16. while (balanced && index < expression.length()) {
  17. char nextCh = expression.charAt(index);
  18. if (isOpen(nextCh)) {
  19. s.push(new Character(nextCh));
  20. }
  21. else if (isClose(nextCh)) {
  22. char topCh = ( (Character) s.pop()).charValue();
  23. balanced = OPEN.indexOf(topCh)
  24. == CLOSE.indexOf(nextCh);
  25. }
  26. index++;
  27. }
  28. }
  29. catch (EmptyStackException ex) {
  30. balanced = false;
  31. }
  32. return (balanced && s.empty());
  33. }
  34.  
  35. private static boolean isOpen(char ch) {
  36. return OPEN.indexOf(ch) > -1;
  37. }
  38.  
  39. private static boolean isClose(char ch) {
  40. return CLOSE.indexOf(ch) > -1;
  41. }
  42.  
  43. public static void main(String args[]) {
  44.  
  45. String expression = JOptionPane.showInputDialog(
  46. "Enter an expression containing parentheses");
  47. if (ParenChecker.isBalanced(expression)) {
  48. JOptionPane.showMessageDialog(null, expression
  49. + " is balanced");
  50. }
  51. else {
  52. JOptionPane.showMessageDialog(null, expression
  53. + " is not balanced");
  54. }
  55. System.exit(0);
  56. }
  57. }

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
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 31
Reputation: scream2ice is an unknown quantity at this point 
Solved Threads: 0
scream2ice scream2ice is offline Offline
Light Poster

Re: Java Stack ... String

 
0
  #5
Feb 12th, 2009
Originally Posted by stephen84s View Post
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!)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Java Stack ... String

 
0
  #6
Feb 12th, 2009
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 5
Reputation: nikolaos is an unknown quantity at this point 
Solved Threads: 0
nikolaos nikolaos is offline Offline
Newbie Poster

Re: Java Stack ... String

 
0
  #7
Feb 12th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 823
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

Re: Java Stack ... String

 
0
  #8
Feb 13th, 2009
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC