Підкажіть , що не так......
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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
Знакомство с тегами
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String nameFile = reader.readLine(); //"/home/viktor/java/er3.txt"; //reader.readLine();
BufferedReader bufferedReader = new BufferedReader(new FileReader(nameFile));
String teg = args[0];
String text = "";
while (bufferedReader.ready()) {
text += bufferedReader.readLine();
}
bufferedReader.close();
reader.close();
String start_line = "<" + teg;
Pattern pattern1 = Pattern.compile(start_line);
Matcher matcher1 = pattern1.matcher(text);
String end_line = "</" + teg;
Pattern pattern2 = Pattern.compile(end_line);
Matcher matcher2 = pattern2.matcher(text);
ArrayList<Integer> start = new ArrayList<>();
ArrayList<Integer> end = new ArrayList<>();
while (matcher1.find()) {
start.add(matcher1.start());
}
while (matcher2.find()) {
end.add(matcher2.start());
}
if (start.size() == end.size()) {
found_Tag(text, teg);
} else {
System.out.println("Є не закриті теги");
}
}
public static String found_Tag(String text, String teg) {
String start_line = "<" + teg;
String end_line = "</" + teg;
String line = "";
List list = new ArrayList<>();
int s = 0;
int r = 0;
int map_Char_start = 0;
int map_Char_end = 0;
String res = "";
List<Integer> arrayX = new ArrayList<>();
List<Integer> arrayY = new ArrayList<>();
for (int i = 0; i < text.length(); i++) {
line += text.charAt(i); // додаю по символу
if (line.endsWith(start_line)) {
map_Char_start = (i - (start_line.length() - 1));
arrayX.add(map_Char_start);
r++;
s = 0;
} else if (line.endsWith(end_line)) {
map_Char_end = (i + 2);
arrayY.add(map_Char_end);
s++;
int x1 = 0;
int y1 = 0;
x1 = arrayX.get(arrayX.size() - s);
y1 = arrayY.get(arrayY.size() - 1);
if (arrayX.size() == arrayY.size()) {
x1 = arrayX.get(0);
y1 = arrayY.get(arrayY.size()-1);
r = 0;
s = 0;
arrayX.clear();
arrayY.clear();
}
list.add(0,text.substring(x1, y1));
}
}
for (int i = 0; i < list.size(); i++) {
res += list.get(i)+ "\n";
}
System.out.println(res);
return res;
}
}