When tested with the suggested input, my code produces the expected output.
However, the code is still wrong, as it does not pass the last requirement. I'm not quite sure why. I understand the problem must be with the last while-loop I have. I might be failing to check for all the possible variations of the input. So, I'd be grateful for criticism and advice.
package com.codegym.task.task19.task1918;
/*
Introducing tags
*/
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.TreeSet;
public class Solution {
public static void main(String[] args) throws IOException, IndexOutOfBoundsException {
//Read from the console the name of a file containing HTML.
///*
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String fileName = br.readLine();
br.close();
//The main method's first parameter is a tag name. For example, "span".
if (args.length < 1) return;
String tag = args[0];
StringBuilder sb = null;
String line = null;
try (BufferedReader fbr = new BufferedReader(new FileReader(fileName))) {
while ((line = fbr.readLine()) != null) {
sb.append(line);
}
} //*/
//two testing lines below:
//StringBuilder sb = new StringBuilder("Info about Leela <span xml:lang=\\\"en\\\" lang=\\\"en\\\"><b><span>Leela Turanga </span></b></span><span>Super</span><span>girl</span>"); //testing line
//String tag = "span";
String tagStart = "<" + tag;
String tagEnd = "</" + tag + ">";
List<Integer> beginIndexes = new ArrayList<>();
int fromIndex = 0;
int beginIndex = 0;
String inputString = sb.toString();
while (fromIndex < inputString.length()) {
if (inputString.substring(fromIndex).contains(tagStart)) {
beginIndex = inputString.indexOf(tagStart, fromIndex);
beginIndexes.add(beginIndex);
fromIndex = beginIndex + tagStart.length();
} else break;
}
List<Integer> endIndexes = new ArrayList<>();
int endIndex = 0;
int from = 0;
//System.out.println("\nThese are the indexes of " + tagEnd + " :"); //for testing
while (from < inputString.length()) {
if (inputString.substring(from).contains(tagEnd)) {
endIndex = inputString.indexOf(tagEnd, from);
endIndexes.add(endIndex);
//System.out.println(endIndex); //for testing
from = endIndex + tagEnd.length();
} else break;
}
TreeSet<String> tagSet = new TreeSet<>();
String tagString = null;
String nestedTagString = null;
int i = 0;
while (i < beginIndexes.size() - 1) {
if (beginIndexes.get(i) < endIndexes.get(i) && endIndexes.get(i) < beginIndexes.get(i+1)) {
tagString = inputString.substring(beginIndexes.get(i), endIndexes.get(i) + tagEnd.length());
tagSet.add(tagString);
i++;
} else {
tagString = inputString.substring(beginIndexes.get(i), endIndexes.get(i+1) + tagEnd.length());
nestedTagString = inputString.substring(beginIndexes.get(i+1), endIndexes.get(i) + tagEnd.length());
tagSet.add(tagString);
tagSet.add(nestedTagString);
i+=2;
}
}
String lastString = inputString.substring(beginIndexes.get(beginIndexes.size()-1), endIndexes.get(endIndexes.size() - 1) + tagEnd.length());
tagSet.add(lastString);
for (String ts: tagSet) {
System.out.println(ts);
}
}
}