I can send you my email if you send me a message to avoid the issue of posting html code which seems to be an issue.
I tried many ways to fail the the code and can't get any errors. Any help would be appreciated
package com.codegym.task.task19.task1918;
/*
Introducing tags
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String filePath = reader.readLine();
reader.close();
BufferedReader reader1 = new BufferedReader(new FileReader(filePath));
// make all the lines into one line to avoid \n & \r
String line = "";
while (reader1.ready()) {
line += reader1.readLine();
}
reader1.close();
//start looking for tag name and check the if the character before is "<" or "/"
//"<" mean it's open tag, and "/" mean close tag. Assign 1 to open and -1 to close.
//when the sum of the tag is zero that mean we had a full html line and will need to print
//along the way, if the we get more than one tag name = nested tags. will need to rerun that html line through the
//same logic( built the same logic like the one above as a separate model to avoid confusing and long lines)
int nestedTag =0;
String tagline;
int tagSum=0;
int start=0;
int lineStart=line.indexOf(args[0], start)-1;// initiate printing starting point which is first tag name,;
int tagNameIndex;
while (true) {
if (line.indexOf(args[0], start) != -1) {//check if there are any more tag name to go through the logic below
tagNameIndex = line.indexOf(args[0], start);
if(tagType(line, tagNameIndex) ==1){ // check if open tag only, open = 1, then counted for the nested tag
nestedTag+=tagType(line, tagNameIndex);
tagSum +=tagType(line, tagNameIndex);// assign 1 to open tags, -1 to close tags
}
else {
tagSum += tagType(line, tagNameIndex);// assign 1 to open tags, -1 to close tags
}
//if(nestedTag > 1){ //check if we got nested tags
// nestedTag = tagSum-1;//less one to count only nested not all tags from full html line
//}
if( tagSum ==0){ //zero indicate a full html line
start=tagNameIndex+args[0].length()+1; //change starting point to the last closed tag name + 1 for ">"
tagline = line.substring(lineStart, start);
System.out.println(tagline);
//go through same logic as above for nested tags. loop to get more than one nested tag
// while stops at 1 to avoid first tage which is captured from above
while (nestedTag > 1){
tagline =nestedLineExtract(tagline, args[0]);
nestedTag--;
}
lineStart = start;
}
start = tagNameIndex+args[0].length()+1;// changer starting point to after lst known tag name index
} else {
break;
}
}
}
public static int tagType(String line, int index){
//check to see if there is one index available before it
int strigAvailableIndexs = line.length();
boolean enoughSpace;
int tagType=0; // if tag type is <open> then 1 if tag type is </close> then -1. the sum is zero and a full line
//check if there is enough space to check one index before. avoid error and lon try and catch routine
if( index >0 & index < strigAvailableIndexs){
enoughSpace = true;
}
else
enoughSpace = false;
if( line.substring((index-1),index).equals("<") & enoughSpace){
tagType = 1;
}
else if( line.substring((index-1),index).equals("/") & enoughSpace){
if( index-2 >= 0){
if( line.substring((index-2),index).equals("</")){
tagType = -1;
}
}
else
tagType=0;
}
return tagType;
}
public static String nestedLineExtract(String line, String tagName){
String tagline="";
int tagSum=0;
int start= line.indexOf(tagName)+tagName.length();// initiate starting point to ignore first tag since it was already been printed;
int lineStart=line.indexOf(tagName, start)-1;
int tagNameIndex;
while (true) {
if (line.indexOf(tagName, start) != -1) {
tagNameIndex = line.indexOf(tagName, start);
tagSum +=tagType(line, tagNameIndex);
if( tagSum ==0){
start=tagNameIndex+tagName.length()+1;
tagline=line.substring(lineStart, start);
System.out.println(tagline);
lineStart = start;
}
start = tagNameIndex+tagName.length()+1;
} else {
break;
}
}
return tagline;
}
}