Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA need help with validPage() HTML is not valid if start tags and end tags are

ID: 3854413 • Letter: J

Question

JAVA need help with validPage()

HTML is not valid if start tags and end tags are mixed in the incorrect order. For example li>This course is about In this case, the li end tag precedes the b tag, but since the boldface element Java language is one of the list items, /b» should precede /l Your task Yor job is to complete the validPage method of the HTMLChecker class. Note that this method is passed one parameter, which is a String. The String contains HTML. The method returns true or false, depending on whether or not the String consists of valid HTML or not. Your program must do the following: 1. Identify tags. You may assume that whenever the". You may assume that the symbols and > will not appear in the String unless they start or end a tag. 2. When your program encounters a type a" or "type b" start tag, it should push the name of the tag onto a stack. For example, when it encounters the html- tag in the text above, it pushes the string "html" onto the stack (since only start tags will be placed on the stack, I suggest that you strip the> from the tag. However, for type c start tags such as cbr> (which have no corresponding end tag), the tag name should not be pushed onto the stack. 3. When your program encounters an end tag, ters an end tag, there a are several possibilities If the stack is empty, then the HTML is invalid, and the validPage method should return false immediately. Otherwise, pop the stack. If the (popped) start tag matches the end tag, then the program should continue processing the rest of the Sring. It the start tag and end tag do not match, then it depends on whether or not the start tag is "type or "type b". In the case of type a" tags, which require a corresponding end tag., your method should return false. For "type b" tags, the the stack should continue to be popped until Ci) it is empty (in which case your method should return false); or (b) until a matching start tag is found (in which case your method should continue pr a. b. c. ocessing the String) 4. If your program reaches the end of the HTML String, then it should retum true of the stack is empty or if every remaining start tag on the stack if type b"

Explanation / Answer

public class HTMLChecker {

private static String[] typeA = {"b", "h1", "h2", "h3", "ol", "ul"};
private static String[] typeB = {"p", "li"};
private static String[] typeC = {"br", "hr", "body", "html","head","title"};

public static boolean validPage(String html) {
Stack<String> startTags = new Stack<String>();
int tagStartPosition = html.indexOf('<');
boolean isStarting;
while (tagStartPosition != -1) {
int tagEndPosition = html.indexOf('>', tagStartPosition + 1);
if (tagEndPosition == -1) {
return false;
}
  
String tag = html.substring(tagStartPosition + 1, tagEndPosition);

if (tag.charAt(0) == '/') {
tag = tag.substring(1);
isStarting = false;
} else {
isStarting = true;
}
if(isStarting){
if(!(isTypeA(tag) ||isTypeB(tag) || isTypeC(tag))) {
return false;
}
}
tagStartPosition = html.indexOf('<', tagEndPosition + 1);

}
return true;
}

private static boolean isTypeA(String tag) {
for (String t : typeA) {
if (tag.equals(t)) {
return true;
}
}
return false;
}

private static boolean isTypeB(String tag) {
for (String t : typeB) {
if (tag.equals(t)) {
return true;
}
}
return false;
}

private static boolean isTypeC(String tag) {
for (String t : typeC) {
if (tag.equals(t)) {
return true;
}
}
return false;
}
public static void main(String args[]) {
String str = "<html><body><h1>asd</h1></body></html>";
if(validPage(str)){
System.out.println("valid page");
}
else{
System.out.println("invalid page");
}
}
}

OUTPUT

run:
valid page
BUILD SUCCESSFUL (total time: 0 seconds)