Please help
package com.codegym.task.task19.task1918;
/*
Introducing tags
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
boolean test = true;
String tag;
if (test)
tag = args[0];
else
tag = reader.readLine();
reader.close();
String openingTag = ("<" + tag);
String closingTag = ("</" + tag + ">");
String fileContents = fileContents(fileName);
char[] openingTagChar = openingTag.toCharArray();
char[] closingTagChar = closingTag.toCharArray();
char[] fileContentsChar = fileContents.toCharArray();
ArrayList<Integer> openingTagIndexes = tagLocation(fileContentsChar, openingTagChar);
ArrayList<Integer> closingTagIndexes = tagLocation(fileContentsChar, closingTagChar);
ArrayList<String> allTags = allTags(fileContentsChar, openingTagIndexes, closingTagIndexes, closingTag);
for (String s : allTags)
System.out.println(s);
}
//return file contents as single string
public static String fileContents(String fileName) throws Exception{
StringBuilder fileContents = new StringBuilder();
String s;
BufferedReader reader = new BufferedReader(new FileReader(fileName));
while ((s=reader.readLine())!=null) {
fileContents.append(s);
}
reader.close();
return fileContents.toString();
}
//return an ArrayList of tag locations
public static ArrayList<Integer> tagLocation(char[] fileContents, char[] tag){
ArrayList<Integer> tagLocation = new ArrayList<>();
int tagIndex = 0;
for (int i = 0; i<fileContents.length; i++){
if (fileContents[i]==tag[tagIndex]){
tagIndex++;
if (tagIndex==tag.length){
tagLocation.add(i-tag.length+1);
tagIndex = 0;
}
} else
tagIndex = 0;
}
return tagLocation;
}
//create an ArrayList of all complete tags
public static ArrayList<String> allTags(char[] fileContents, ArrayList<Integer> openingTags, ArrayList<Integer> closingTags, String closingTag){
ArrayList<String> allTags = new ArrayList<>();
while (!openingTags.isEmpty()) {
int openingTagIndex = 0;
StringBuilder stringBuilder = new StringBuilder();
while (openingTags.size()>1 && openingTags.get(openingTagIndex) < closingTags.get(0)) {
openingTagIndex++;
}
if (openingTagIndex>0)
openingTagIndex--;
for (int i = openingTags.get(0); i < closingTags.get(openingTagIndex); i++) {
stringBuilder.append(fileContents[i]);
}
stringBuilder.append(closingTag);
allTags.add(stringBuilder.toString());
openingTags.remove(0);
closingTags.remove(openingTagIndex);
}
return allTags;
}
}