I ran the program using the example and it displayed nothing
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<Integer> nestedTags = nestedTags(openingTagIndexes, closingTagIndexes);
ArrayList<String> allTags = allTags(fileContentsChar, openingTagIndexes, closingTagIndexes, nestedTags, 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[1]==tag[tagIndex]){
tagIndex++;
if (tagIndex==tag.length){
tagLocation.add(i-tag.length+1);
tagIndex = 0;
}
} else
tagIndex = 0;
}
return tagLocation;
}
//create an ArrayList to see if there are any nested tags
public static ArrayList<Integer> nestedTags(ArrayList<Integer> openingTags, ArrayList<Integer> closingTags){
ArrayList<Integer> nestedTags = new ArrayList<>();
int openingTagIndex = 0;
int closingTagIndex = 0;
int nestedTagsCount = 0;
for (int i = 0; i < openingTags.size(); i++){
while (openingTags.get(openingTagIndex) < closingTags.get(closingTagIndex)){
openingTagIndex++;
nestedTagsCount++;
}
openingTagIndex = i+1;
closingTagIndex = i+1;
nestedTags.add(nestedTagsCount);
nestedTagsCount = 0;
}
return nestedTags;
}
//return an ArrayList<String> of all complete tags by iterating through filecontents char[]
//starting from the opening tag index
//and reading to the closing tag index
//which is found by adding the equivalent (nestedTagCount - 1)
//to the equivalent closingTag
public static ArrayList<String> allTags(char[] fileContentsChar, ArrayList<Integer> openingTagIndexes, ArrayList<Integer> closingTagIndexes, ArrayList<Integer> nestedTags, String closingTag){
ArrayList<String> allTags = new ArrayList<>();
for (int i = 0; i < openingTagIndexes.size(); i++){
StringBuilder stringBuilder = new StringBuilder();
int closingIndex = closingTagIndexes.get(nestedTags.get(i)-1);
for (int j = openingTagIndexes.get(i); j < closingIndex; j++){
String s = String.valueOf(fileContentsChar[j]);
stringBuilder.append(s);
}
stringBuilder.append(closingTag);
allTags.add(stringBuilder.toString());
}
return allTags;
}
}