I tried countless times, looked a few times in the help section, tested it with a few cases, and still it won't validate.
The code handles even nested within nested within nested tags (x3).
Does anyone have any suggestions or test cases I can try?
Thank you very much!
package com.codegym.task.task19.task1918;
/*
Introducing tags
*/
import java.io.*;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
reader.close();
BufferedReader bufferedFileReader = new BufferedReader(new FileReader(fileName));
StringBuilder stringBuilder = new StringBuilder();
while (bufferedFileReader.ready()) {
stringBuilder.append(bufferedFileReader.readLine());
}
bufferedFileReader.close();
String fileContents = stringBuilder.toString().replaceAll("\r\n", "");
String openingTag = "<" + args[0];
String closingTag = "</" + args[0] + ">";
ArrayList<String> tags = new ArrayList<>();
while (fileContents.indexOf(openingTag) != -1) {
String nestedTag2 = "";
String nestedTag3 = "";
String tag = fileContents.substring(fileContents.indexOf(openingTag), fileContents.indexOf(closingTag) + args[0].length() + 3);
if (tag.indexOf(openingTag, args[0].length()) != -1) {
nestedTag2 = fileContents.substring(fileContents.indexOf(openingTag), fileContents.indexOf(closingTag, fileContents.indexOf(closingTag) + args[0].length() + 1) + args[0].length() + 3);
if (tag.indexOf(openingTag, tag.indexOf(openingTag, args[0].length())) != -1) {
nestedTag3 = fileContents.substring(fileContents.indexOf(openingTag), fileContents.indexOf(closingTag,fileContents.indexOf(closingTag, fileContents.indexOf(closingTag) + args[0].length() + 1) + args[0].length() + 1) + args[0].length() + 3 );
tags.add(nestedTag3);
tags.add(nestedTag2.substring(nestedTag2.indexOf(openingTag, nestedTag2.indexOf(openingTag) + args[0].length() + 1)));
tags.add(nestedTag2.substring(nestedTag2.indexOf(openingTag, nestedTag2.indexOf(openingTag, nestedTag2.indexOf(openingTag) + args[0].length() + 1) + args[0].length() + 1), nestedTag2.indexOf(closingTag) + args[0].length() + 3));
fileContents = fileContents.substring(fileContents.indexOf(nestedTag3) + nestedTag3.length());
} else {
tags.add(nestedTag2);
tags.add(nestedTag2.substring(nestedTag2.indexOf(openingTag) + args[0].length() + 1, nestedTag2.indexOf(closingTag)));
fileContents = fileContents.substring(fileContents.indexOf(nestedTag2) + nestedTag2.length());
}
} else {
tags.add(tag);
fileContents = fileContents.substring(fileContents.indexOf(tag) + tag.length());
}
}
for (String tag: tags
) {
System.out.println(tag);
}
}
}