Не могу понять в чем проблема, понимаю что придумал велосипед, но все на теги разбивается правильно вроде, проверял на разных примеры и из комментарий тоже. Очень много времени потратил, валидатор не пропускает, подскажите что не так или на каком примере проверить?
package com.javarush.task.task19.task1918;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Знакомство с тегами
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedReader reader1 = new BufferedReader(new FileReader(reader.readLine()));
reader.close();
String t = args[0];
String teg = "<" + t;
String endteg = "/" + t + ">";
StringBuilder str = new StringBuilder();
while (reader1.ready()) {
str.append(reader1.readLine());
}
reader1.close();
String s = str.toString();
String result = "";
String[] strings1 = s.split(teg);
String[] strings = new String[strings1.length - 1];
for (int i = 1; i < strings1.length; i++) {
strings[i - 1] = strings1[i];
}
strings1 = null;
strings[strings.length-1] = endString(strings[strings.length-1], endteg);
List<StringTeg> integers = new ArrayList<>();
List<String> list = new ArrayList<>();
int count = 0;
int dl = 0;
for (int i = 0; i < strings.length; i++) {
if (strings[i].contains(endteg)) {
int a = isRaz(strings[i], endteg);
if (a == 1 && count == 0) {
integers.add(new StringTeg(i, 0, a));
} else if (a == 1 && count > 0) {
dl++;
integers.add(new StringTeg(i, 0, a));
} else if (a - 1 == count) {
integers.add(new StringTeg(i - dl, dl+1, a));
count = 0;
dl = 0;
} else if (a - 1 < count) {
integers.add(new StringTeg(i - (a - 1), a, a));
count = count - (a - 1);
dl++;
}
} else {
dl++;
count++;
}
}
for (String st : strings){
list.add(st);
}
for (StringTeg st : integers) {
if (st.countteg == 1) {
String s1 = teg + strings[st.pos];
list.set(st.pos, s1);
} else {
int b = st.pos;
int d = st.dl;
String s1 = "";
for (int j = b; j < b+d; j++) {
s1 = s1 + teg + strings[j];
}
list.set(st.pos, s1);
int p = b + d-(st.countteg-1);
for (int i = 1; i < st.countteg; i++) {
String s2 = "";
String[] endstring = endSplit(strings[st.pos+ st.dl-1], endteg);
int k = 0;
for (int j = b+d-1; j >= p; j--) {
if (s2.equals("")) {
s2 = teg + endstring[k];
} else {
s2 = teg + strings[j] + s2 + endstring[k];
}
k++;
}
list.set(p, s2);
p++;
}
}
}
for (String st : list) {
System.out.println(st);
}
/*String s = "</span></span></span>+4656356";
s = endString(s, "/span>");
System.out.println(s);
*/
}
public static boolean isOne(String s, String endteg) {
String check = s.replaceFirst(endteg, "");
boolean b = false;
if (check.contains(endteg)) {
b = false;
} else {
b = true;
}
return b;
}
public static int isRaz(String s, String endteg) {
int count = 1;
boolean b = true;
while (b) {
String check = s.replaceFirst(endteg, "");
s = check;
if (check.contains(endteg)) {
count++;
} else {
b = false;
}
}
return count;
}
public static String[] endSplit(String s, String endteg) {
String[] strings = s.split(endteg);
for (int i = 0; i < strings.length; i++) {
strings[i] = strings[i] + endteg;
}
return strings;
}
public static String endString(String s, String endteg){
String[] str = endSplit(s, endteg);
String result = "";
if (isRaz(s,endteg) == str.length){
for(String st : str){
result = result + st;
}
} else {
str[str.length-1] = "";
for(String st : str){
result = result + st;
}
}
return result;
}
}