In class this week, we will develop a Stack class and a Queue class. Use these c
ID: 650381 • Letter: I
Question
In class this week, we will develop a Stack class and a Queue class. Use these classes to implement an HTML balanced tag checker. Your assignment is to write a program that prompts the user for an .html file name, opens the file and scans it to ensure that each start tag has a matching end tag. This is a simple html file with properly matched tags. The opening tag and closing tag match by name, with the closing tag beginning with a /.
This is my first web page
1. It is acceptable for tags to be nested.
2.To make the assignment more manageable, we will assume that each tag must be opened and closed (true in XHTML but not HTML).
3.Tag matches should be case insensitive. It is ok to convert all tags to lower (or upper) case.
4.You should prompt the user for a filename. If that file does not exist, you may exit gracefully, you do not need to ask again The objectives of this assignment do not include practice with file parsing. To that end, you may make the following (some realistic, some unrealistic) assumptions:
1. tags can not cross multiple lines
2. tags may contain no white space
3. tags may have no attributes, they are one, single word (or character)
Plan of Attack: Create a Tag class capable of holding the information of one tag. To make life easier, you'll want a Tag to be able to identify its base tag (ie, b, body, html) as well as whether it's an opening or closing tag.
The algorithm: Load all tags from the file into a queue Create a stack for each tag in the queue if it is an opening tag push it onto the stack if it is a closing tag, if stack is empty MISMATCH otherwise pop the top element from the stack - and you should have a match if match keep going otherwise MISMATCH If you find a mismatch, report the error and terminate the program.
Provided Classes:
If your program was run on this file:
The output would be:
</b> matches <b>
Found </body> to match <html> terminating program
If your program was run on this file:
<HTML>
<HEAD>
<TITLE>My Web Page</TITLE>
</HEAD>
<BODY>
<P>This is where you will enter all the text and images you want displayed in a browser window.</P>
</BODY>
</HTML>
The output would be:
</title> matches <title> </head> matches <head> </p> matches <p>
</body> matches <body> </html> matches <html> All tags in file matched
What to submit :
Explanation / Answer
import java.util.Arrays;
import java.util.Scanner;
import java.io.FileReader;
public class TagBalancer {
public static void main ( String args[] )
{
System.out.println("Enter a .html file name");
Scanner in = new Scanner(System.in);
String a=in.nextLine();
Scanner fileInput = new Scanner(new FileReader("filename.txt"));
QueueReferenceBased q= new QueueReferenceBased();
while(fileInput.hasNext())
{
String tempe=fileInput.nextLine();
if(tempe.charAt(0)=='<')
{
Tag tagVal=new Tag(tempe);
q.enqueue(tagVal);
}
}
StackReferenceBased s=new StackReferenceBased();
while(!q.isEmpty())
{
Tag tagVal=q.dequeue();
if(tagVal.opening)
{
s.push(tagVal);
}
else
{
if(!s.isEmpty())
s.pop();
else
{
System.out.println("MISMATCH");
break;
}
}
}
}
}
public class Tag{
String base;
boolean opening;
Tag(String tempe)
{
if(tempe.charAt(1)=='/')
{
base=tempe.substring(2,tempe.length()-1);
opening=false;
}
else
{
base=tempe.substring(1,tempe.length()-1);
opening=true;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.