Could you plz help me with the final condition: have tried many different options, still cant pass the validation
package com.codegym.task.task19.task1918;
/*
Introducing tags
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
String file= reader.readLine();
//String file = "C:\\Users\\k.shayakhmetov\\Desktop\\CodeGym\\CodeGymTasks\\CodeGymTasks\\2.JavaCore\\src\\com\\codegym\\task\\task19\\task1928\\file";
reader.close();
//read the file content
BufferedReader reader1 = new BufferedReader(new FileReader(file));
StringBuilder content = new StringBuilder();
while (reader1.ready()) {
content.append(reader1.readLine());
}
reader1.close();//close the stream
content = new StringBuilder(content.toString().replaceAll("\\n", ""));
content = new StringBuilder(content.toString().replaceAll("\\r", ""));
String tagPattern = args[0]; //the tag we are looking for
String openTag = "<" + tagPattern;
String closeTag = "</" + tagPattern + ">";
int index = 0;
int openTagCount = 0;
int closeTagCount = 0;
ArrayList<Integer> openTagIndexList = new ArrayList<>();
ArrayList<Integer> closeTagIndexList = new ArrayList<>();
StringBuilder output = new StringBuilder();
while (index < content.length()) {
if (content.charAt(index) == '<') {
if (content.substring(index, index + openTag.length()).equals(openTag)) {
output.append(content.substring(index, index + openTag.length()));
openTagCount++;
openTagIndexList.add(index);
index = index + openTag.length();
} else if (content.substring(index, index + closeTag.length()).equals(closeTag)) {
output.append(content.substring(index, index + closeTag.length()));
closeTagCount++;
closeTagIndexList.add(index);
index = index + closeTag.length();
} else {
output.append(content.charAt(index));
index++;
}
} else {
output.append(content.charAt(index));
index++;
}
if (openTagCount==closeTagCount){
if (openTagCount==1){
System.out.println(output);
output=new StringBuilder();
openTagCount=0;
closeTagCount=0;
openTagIndexList=new ArrayList<>();
closeTagIndexList=new ArrayList<>();
}
else if (openTagCount>1){
System.out.println(output);
for (int i=0;i<openTagIndexList.size()-1;i++) {
String s = content.substring(openTagIndexList.get(openTagIndexList.size()-1-i),closeTagIndexList.get(i))+closeTag;
System.out.println(s);
s="";
}
output=new StringBuilder();
openTagCount=0;
closeTagCount=0;
openTagIndexList=new ArrayList<>();
closeTagIndexList=new ArrayList<>();
}
}
}
}
}