| | |
Java Stack ... String
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2008
Posts: 31
Reputation:
Solved Threads: 0
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
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!
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
Java Syntax (Toggle Plain Text)
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!
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.
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 ?"
"How to ask questions the smart way ?"
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.
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 !!!
•
•
Join Date: Mar 2008
Posts: 31
Reputation:
Solved Threads: 0
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:
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
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:
Java Syntax (Toggle Plain Text)
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
•
•
Join Date: Mar 2008
Posts: 31
Reputation:
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.
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.
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 !!!
•
•
Join Date: Nov 2007
Posts: 5
Reputation:
Solved Threads: 0
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.
<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.
![]() |
Similar Threads
- Isn't java pass by reference? (Java)
- Java Simple calc (Java)
- I created another Stack that tells if word is palindrome, need help (Java)
- Stack- need help (Java)
- palindrome (Java)
- line up JTextField GUI java & write to file (Java)
- Trying to create a method to convert string letters into a two dimensional array (Java)
- Java with String Tokenizer (Java)
Other Threads in the Java Forum
- Previous Thread: How to indent in java
- Next Thread: Garbage collection in java
Views: 1268 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
add android api apple applet application arguments array arrays automation binary bluetooth chat chooser class classes client code component converter database digit draw eclipse equation error event exception file fractal functiontesting game givemetehcodez graphics gui health helpwithhomework html hyper ide idea image input int integer j2me jarfile java javame javaprojects jmf jni jpanel julia linux list loop main map method methods mobile myregfun netbeans newbie nonstatic number object oracle pattern pearl print problem program programming project recursion scanner screen scrollbar server set size sms socket sort sorting spamblocker sql sqlserver string superclass swing test text-file thread threads time tree windows






