Тут уже все работает. Ну валидатор не пускает.
Согласен велосипед еще тот, но она меня конкретно задолбала.
package com.javarush.task.task19.task1918;
/*
Знакомство с тегами
*/
import javax.swing.text.Document;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.FileNotFoundException;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static void main(String[] args) {
String nameFile;
String t = args[0];
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
nameFile = br.readLine();
} catch (IOException e) {
System.out.println("Not assign a nameFile");
return;
}
try (BufferedReader br = new BufferedReader(new FileReader(nameFile))) {
StringBuilder sb = new StringBuilder();
while (br.ready()) {
sb.append(br.readLine());
}
Pattern pt = Pattern.compile("<" + t + "\\s+.*?>|<" + t + ">|<\\/" + t + ">");
Matcher mt = pt.matcher(sb);
List<String> list = new ArrayList<>();
while (mt.find()) {
list.add(mt.group(0));
}
/*for (String a : list) {
System.out.println(a);
}*/
int i = 0;
while (!(list.isEmpty())) {
if (Pattern.matches("<" + t + ">", list.get(i)) && Pattern.matches("<\\/" + t + ">", list.get(i + 1))) {
System.out.println(list.get(i) + list.get(i + 1));
list.remove(i);
list.remove(i);
i = 0;
}
else if (Pattern.matches("<" + t + "\\s+.*?>", list.get(i)) && Pattern.matches("<\\/" + t + ">", list.get(i + 1))) {
list.set(i, list.get(i).replaceAll("\\s+.*?>", ">"));
System.out.println(list.get(i) + list.get(i + 1));
list.remove(i);
list.remove(i);
i = 0;
}
else {
i++;
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} catch (IOException e) {
System.out.println("IOException error");
}
}
}