Hey, guys! Maybe, someone could help me out with this. This code returns a correct solution for all mentioned examples ("a" tag, nested tags), but still doesn't verify. Guess, I'm missing some variant of an input file. Thanks!
package com.codegym.task.task19.task1918;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
/*
Introducing tags
*/
public class Solution {
//public static List<String> res = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));
String fileName = myInput.readLine();
//String fileName = "h:\\file1.txt";
myInput.close();
BufferedReader myFile = new BufferedReader (new FileReader(fileName));
String[] res = new String[100];
String interim = "";
char ongoing;
int openedTags = 0, closedTags = 0;
boolean startWrite = false;
boolean[] resWrite = new boolean[100];
while (myFile.ready()) {
ongoing = (char) myFile.read();
if (ongoing == '<') startWrite = true;
if (startWrite) interim += ongoing;
if (interim.equals("<" + args[0])) {
res[openedTags] = "";
res[openedTags] += interim.substring(0,interim.length()-1);
resWrite[openedTags] = true;
interim = "";
openedTags += 1;
startWrite = false;
}
for (int i = 0; i < openedTags; i++) {
if (resWrite[i]) res[i] += ongoing;
}
if (interim.equals("</" + args[0] + ">")) {
closedTags += 1;
resWrite[openedTags-closedTags] = false;
if (openedTags - closedTags == 0) closedTags = 0;
}
if (!interim.equals("<" + args[0]) & ongoing == '>') {
interim = "";
startWrite = false;
}
}
//System.out.println(Arrays.toString(res));
for (int i = 0; i < res.length; i++) {
if (res[i] != null) {
res[i] = res[i].replaceAll("\r\n","");
System.out.println(res[i]);
}
}
myFile.close();
}
}