This is 10th time I'm getting the correct answer, even by giving more complicated inputs comparing to CodeGym's example.
My code can run over nested HTML, even finds tags nested in another more than once. But it's not getting verified.
If there's something specific our code needs to do to be verified, why not mention it in descriptions?
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 {
private static String theActualStartingTag;
private static String theActualEndingTag;
public static void main(String[] args) throws IOException {
BufferedReader bfFileNameReader = new BufferedReader(new InputStreamReader(System.in));
String fileName = bfFileNameReader.readLine();
bfFileNameReader.close();
StringBuilder tagBuilder = new StringBuilder();
tagBuilder.append("<");
tagBuilder.append(args[0]);
theActualStartingTag = tagBuilder.toString();
tagBuilder.insert(1, "/");
tagBuilder.append(">");
theActualEndingTag = tagBuilder.toString();
BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
StringBuilder wholeFileString = new StringBuilder();
while (fileReader.ready()){
wholeFileString.append((char) fileReader.read());
}
fileReader.close();
lineProcessor(lineTrimmer(wholeFileString.toString()));
}
private static void lineProcessor(String line){
int nestDepth = 0;
StringBuilder oneTaggedLine = new StringBuilder();
char[] trimmedLineCharArray = line.toCharArray();
for (char c : trimmedLineCharArray) {
oneTaggedLine.append(c);
if (oneTaggedLine.length() >= theActualStartingTag.length() && oneTaggedLine.toString()
.substring(oneTaggedLine.length() - theActualStartingTag.length()).equals(theActualStartingTag)) {
nestDepth++;
} else if (oneTaggedLine.length() >= theActualEndingTag.length() && oneTaggedLine.toString()
.substring(oneTaggedLine.length() - theActualEndingTag.length()).equals(theActualEndingTag)) {
nestDepth--;
if (nestDepth == 0) {
System.out.println(oneTaggedLine.toString());
if (oneTaggedLine.toString().substring(1, oneTaggedLine.length()).contains(theActualStartingTag)) {
oneTaggedLine.deleteCharAt(0);
oneTaggedLine.deleteCharAt(oneTaggedLine.length()-1); //make the string ready to be trimmed
lineProcessor(lineTrimmer(oneTaggedLine.toString())); //use lineProcessor recursively
}
oneTaggedLine.delete(0, oneTaggedLine.length());
}
}
}
}
private static String lineTrimmer(String lineString){
StringBuilder processedLine = new StringBuilder();
processedLine.append(lineString);
//make sure the String is begins and ends with the desired tag
while(processedLine.length() >= theActualStartingTag.length()) {
if (!processedLine.toString().substring(0, theActualStartingTag.length()).equals(theActualStartingTag)) {
processedLine.deleteCharAt(0);
}else break;
}
while(processedLine.length() >= theActualEndingTag.length()) {
if (!processedLine.toString().substring(processedLine.length() - theActualEndingTag.length()).equals(theActualEndingTag)) {
processedLine.deleteCharAt(processedLine.length()-1);
}else break;
}
return processedLine.toString();
}
}