Despite my solution working with the main example, plus it is working with multiple nested tags (2+) and other tags different from the main one.
package com.codegym.task.task19.task1918;
/*
Introducing tags
*/
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
BufferedReader fileReader = new BufferedReader(new FileReader(consoleReader.readLine()));
consoleReader.close();
StringBuilder sb = new StringBuilder();
while (fileReader.ready()) {
sb.append(fileReader.readLine());
}
fileReader.close();
String[] s = sb.toString().replaceAll("\n", "").replaceAll("\r", "").split("<");
String openingtag = "<" + args[0];
String closingtag = "</" + args[0] + ">";
char open = '阿';
String openSym = String.valueOf(open);
char close = '不';
String closeSym = String.valueOf(close);
ArrayList<String> html = new ArrayList<>();
for (int i = 1; i < s.length; i++) {
String temp = "<" + s[i];
html.add(temp.replaceAll(openingtag, String.valueOf(open)).replaceAll(closingtag, String.valueOf(close)));
}
// init opening and closing tag counts
int countOpen = 0;
int countClose = 0;
// list of StringBuilders to store all the tags plus the nested ones
List<StringBuilder> tags = new ArrayList<>();
// iterating through html list containing split information
for (int i = 0; i < html.size(); i++) {
String current = html.get(i);
// checking if current contains opening tag
if (current.contains(openSym)) {
// increasing countOpen
countOpen++;
// checking if countOpen > countClose, to init new StringBuilder, thus checking how many nested tag there are
if (countOpen > countClose) {
tags.add(new StringBuilder());
}
// appending all the StringBuilders in tags
for (int j = 0; j < tags.size(); j++) {
tags.get(j).append(current.replace(openSym, openingtag));
}
} // checking for closing tags
else if (current.contains(closeSym)) {
// increasing countClose
countClose++;
// appending all the StringBuilders in tags - countClose, so we appropriately close the nested ones
for (int j = 0; j <= tags.size() - countClose; j++) {
tags.get(j).append(current.replace(closeSym, closingtag));
}
} // this is if there is only one opened tag
else if (current.contains(closeSym) && countOpen == 1) {
countClose++;
for (int j = 0; j < tags.size(); j++) {
tags.get(j).append(current.replace(closeSym, closingtag));
}
} // this is for other tags
else if (!current.contains(openSym) && !current.contains(closeSym)) {
for (int j = 0; j < tags.size() - countClose; j++) {
tags.get(j).append(current);
}
}
// checking if countOpen equals countClose, basically this happens after the first opened tag is closed, thus creating a line
if (countOpen == countClose) {
for (int j = 0; j < tags.size(); j++) {
System.out.println(tags.get(j).toString());
}
// reseting tags and counts
tags.clear();
countOpen = 0;
countClose = 0;
}
}
}
}
/*int counter = 0;
int counterNested = 0;
int plusNested = 0;
StringBuilder str = new StringBuilder();
StringBuilder nested = new StringBuilder();
for (int i = 0; i < html.size(); i++) {
if (counter == 0) {
str = new StringBuilder();
nested = new StringBuilder();
}
if (html.get(i).contains(String.valueOf(open))) {
str.append(html.get(i).replace(String.valueOf(open), openingtag));
counter++;
if (counter > 1) {
nested.append(html.get(i).replace(String.valueOf(open), openingtag));
counterNested++;
plusNested = counterNested;
}
} else if (html.get(i).contains(String.valueOf(close)) && counter > 0) {
str.append(html.get(i).replace(String.valueOf(close), closingtag));
if (counter > 1 && counterNested > 0) {
nested.append(html.get(i).replace(String.valueOf(close), closingtag));
counterNested--;
}
counter--;
} else if (!html.get(i).contains(String.valueOf(open)) && !html.get(i).contains(String.valueOf(close))) {
str.append(html.get(i));
}
if (counter == 0) {
System.out.println(str.toString());
if (nested.toString().contains(openingtag) && nested.toString().contains(closingtag)) {
System.out.println(nested.toString());
for (int j = 0; j < plusNested; j++) {
String newNested = nested.toString().replaceFirst(openingtag + ">", "").replaceFirst(closingtag, "");
System.out.println(newNested);
}
}
}
//if (counter == 0) System.out.println();
}*/