Добрый вечер, коллеги!
Посмотрите, пожалуйста, можно ли как-то поправить этот вариант решения, что бы валидатор принял.
На примере данном в условии - все работает
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.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static void main(String[] args) {
try {
BufferedReader readname = new BufferedReader(new InputStreamReader(System.in));
BufferedReader reader = new BufferedReader(new FileReader(readname.readLine()));
readname.close();
while (reader.ready()) {
String str = reader.readLine();
String teg = "<".concat(args[0]);
String upteg = "</".concat(args[0].concat(">"));
Pattern s = Pattern.compile(teg);
Pattern e = Pattern.compile(upteg);
Matcher start = s.matcher(str);
Matcher end = e.matcher(str);
Stack<Integer> starty = new Stack<>();
while (start.find()) {
end.find();
int b = start.start();
int off = end.end();
int i = b;
String res = str.substring(b, off);
String sustr = str.substring(b, off);
while (sustr.substring(0 + teg.length()).contains(teg)) {
if (start.find() && end.find()) {
b = start.start();
int c = end.end();
sustr = str.substring(b, c);
res = str.substring(i, c);
}
}
if (!res.substring(0 + teg.length()).contains(teg)) {
System.out.println(res);
} else {
System.out.println(res);
res = res.substring(res.indexOf('>'), res.length() - upteg.length());
Matcher mo = s.matcher(res);
while (mo.find()) {
starty.push(mo.start());
}
Matcher f = e.matcher(res);
while (!starty.empty()) {
if (f.find()) {
int j = f.end();
String r = res.substring(starty.pop(), j);
System.out.println(r);
}
}
}
}
}
reader.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}