I'm getting a response that there is an infinite loop, but I have run this in Eclipse with no issues. So I think there is an issue with this problem, not the code.
package com.codegym.task.task19.task1918;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileReader;
import java.util.ArrayList;
/*
Introducing tags
C:\\Users\\jkreis\\Desktop\\test1.txt
Info about Leela <span xml:lang="en" lang="en"><b><span>Leela Turanga</span></b></span><span>Super</span><span>girl</span>
*/
public class Solution {
public static void main(String[] args) throws IOException {
String tagName = args[0];
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
FileReader fr = new FileReader(fileName);
StringBuilder source = new StringBuilder();
int character;
while ((character = fr.read()) != -1) {
if (character != 10 && character != 13)
source.append((char) character);
}
reader.close();
fr.close();
ArrayList<StringBuilder> spans = new ArrayList<StringBuilder>();
String openTag = String.format("<%s", tagName);
String closeTag = String.format("</%s>", tagName);
int i = 0;
while (i < source.length()) {
String next = "";
if (i + openTag.length() <= source.length()) {
if (source.substring(i, i + openTag.length()).matches(openTag)) {
next = openTag;
}
}
if (i + closeTag.length() <= source.length()) {
if (source.substring(i, i + closeTag.length()).matches(closeTag)) {
next = closeTag;
}
}
if (next.equals("")) {
next = source.substring(i, i + 1);
}
for (StringBuilder span : spans) {
if (getOpenSpansWithoutClose(span.toString(), openTag, closeTag) > 0) {
span.append(next);
}
}
if (next.equals(openTag)) {
spans.add(new StringBuilder(next));
}
i += next.length();
}
}
public static int getOpenSpansWithoutClose(String span, String openTag, String closeTag) {
int openCount = 0;
int closeCount = 0;
int index = 0;
while (index < span.length()) {
if (index + openTag.length() <= span.length()) {
if (span.substring(index, index + openTag.length()).equals(openTag)) {
openCount++;
index += openTag.length();
}
}
if (index + closeTag.length() <= span.length()) {
if (span.substring(index, index + closeTag.length()).equals(closeTag)) {
closeCount++;
index += closeTag.length();
}
}
if (index + openTag.length() > span.length()) {
break;
} else {
index++;
}
}
return openCount - closeCount;
}
}