I have tried several inputs and it works fine. But for some reason, it cannot pass the last verification. Can you give me a little hint, please?
Any help will be appreciated!
package com.codegym.task.task19.task1918;
/*
Introducing tags
*/
import java.io.*;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static void main(String[] args) throws IOException {
//for test
/*
String html = "Info about Leela (span xml:lang=\"en\" lang=\"en\")(span)girl(/span)(b)(span)Leela Turanga\n" +
"(/span)(/b)(/span)(span)Super(/span)(span)girl(/span)";
FileOutputStream fis = new FileOutputStream("C:Users/ASUS/Desktop/orinoco.txt");
String output = html.replaceAll("\\(", "<").replaceAll("\\)", ">");
fis.write(output.getBytes());
fis.close();
*/
//
//get fileName
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String fileName = bufferedReader.readLine();
bufferedReader.close();
//read all lines in dataList
BufferedReader reader = new BufferedReader(new FileReader(fileName));
ArrayList<String> dataList = new ArrayList<>();
while (reader.ready()) {
dataList.add(reader.readLine());
}
reader.close();
//find the indexes of opentags and closetags
String tag = args[0];
StringBuilder sb = new StringBuilder();
for (String s : dataList) {
sb.append(s);
}
String data = sb.toString();
Pattern startPattern = Pattern.compile("<"+tag);
Matcher m1 = startPattern.matcher(data);
ArrayList<Integer> startList = new ArrayList<>();
while (m1.find()) {
startList.add(m1.start());
}
Pattern endPattern = Pattern.compile("</"+tag+">");
Matcher m2 = endPattern.matcher(data);
ArrayList<Integer> endList = new ArrayList<>();
while (m2.find()) {
endList.add(m2.end() - 1);
}
/*
int count1, count2;
while (startList.size() > 1) {
count1 = 1; count2 = 0;
while (count1 < startList.size() && count2 < endList.size()) {
if (startList.get(count1) < endList.get(count2)) { //nested tag
count1++;
count2++;
}
else {
System.out.println(data.substring(startList.get(0), endList.get(count2)));
startList.remove(0);
endList.remove(count2);
break;
}
}
}
*/
//go through the string, if i == the index of opentag/closetag, the count++. When count1 == count2, display the substring.
while (startList.size() > 1) {
int count1 = 0, count2 = 0;
for (int i = 0; i < data.length(); i++) {
if (i == startList.get(count1))
count1++;
if (i == endList.get(count2))
count2++;
if (count1 == count2 && count1 != 0) {
System.out.println(data.substring(startList.get(0), endList.get(count2 - 1) + 1));
startList.remove(0);
endList.remove(count2 - 1);
break;
}
}
}
System.out.println(data.substring(startList.get(0), endList.get(0)));
}
}