Please, could somebody check what I am missing here?
The code gives result as in the example but do not pass the testing :(
package com.codegym.task.task19.task1918;
import java.io.*;
import java.util.*;
/*
Introducing tags
<span xml:lang="en" lang="en"><b><span>Leela Turanga</span></b></span><span>Super</span><span>girl</span>
Info about Leela
@ xml:lang="en" lang="en">
<b>
@>Leela Turanga
$
</b>
$
@>Super
$
@>girl
$
<span xml:lang="en" lang="en"><b><span>Leela Turanga</span></b></span>
<span>Leela Turanga</span>
<span>Super</span>
<span>girl</span>
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedReader fileReader = new BufferedReader(new FileReader(reader.readLine()));
reader.close();
StringBuilder stringBuilder = new StringBuilder();
while(fileReader.ready()) {
stringBuilder.append(fileReader.readLine());
}
String[] split = stringBuilder.toString().replaceAll("\n", "").replaceAll("\r", "").split("<");
String openingtag = "<" + args[0];
String closingtag = "</" + args[0] + ">";
ArrayList<String> fileContent = new ArrayList<String>();
for(int i=0; i<split.length; i++){
String temp = "<" + split[i];
fileContent.add(temp);
// System.out.println("ADDED TEMP : " + temp);
}
int openedTagCounter = 0; //how many opened tags there are
int nestedTagCounter = 0; // where the nested tag is located
String toPrint = "";
boolean opened = false;
for(int i = 0; i < fileContent.size(); i++){
// System.out.println("CHECKING: " + fileContent.get(i));
if(fileContent.get(i).contains(openingtag)){
opened = true; //to check if there was an opening tag
toPrint += fileContent.get(i);
if((openedTagCounter > 0) && (nestedTagCounter == 0)) {
nestedTagCounter = i; // to get back to the index of first nested tag
}
openedTagCounter++;
// System.out.println("TO PRINT OPEN TAG : " + toPrint);
}
else if(fileContent.get(i).contains(closingtag) && opened){ //chceking only if there was an opening tag, used in case of nested tags
toPrint += fileContent.get(i);
openedTagCounter--;
// System.out.println("TO PRINT CLOSE TAG : " + toPrint);
}
else if(!(fileContent.get(i).contains(openingtag)) && (openedTagCounter > 0)){
toPrint += fileContent.get(i); //adding things like <br> etc.
// System.out.println("TO PRINT INSIDE : " + toPrint);
}
if((openedTagCounter == 0) && opened){
// System.out.println("------------- " + toPrint);
System.out.println(toPrint);
toPrint = "";
opened = false;
if(nestedTagCounter !=0) {
i = nestedTagCounter - 1;
nestedTagCounter = 0;
}
}
}
// int startindex;
// int endindex;
// int tofind;
//
// while(fileContent.size() > 0) {
// int t=0;
// for(int i=0; i<fileContent.size(); i++) {
// if(fileContent.get(i).contains("阿")) {
// t = i;
// break;
// }
// }
// for(int i=0; i<t; i++) {
// fileContent.remove(0);
// }
//
// startindex = 0;
// tofind = 0;
// endindex = 0;
//
// for(int i=1; i<fileContent.size(); i++) {
// if(fileContent.get(i).contains("阿")) tofind++;
// if(fileContent.get(i).equals("不")) {
// if(tofind != 0) tofind--;
// else if(tofind == 0) {
// endindex = i;
// break;
// }
// }
// }
//
// StringBuilder sbb = new StringBuilder();
//
// for(int i=startindex; i<=endindex; i++) {
// sbb.append(fileContent.get(i));
// }
//
// System.out.println(sbb.toString().replaceAll("阿",openingtag).replaceAll("不",closingtag));
//
// fileContent.remove(0);
// }
fileReader.close();
}
}