I have tried several different data files to try and see what it is I am missing.
Can't figure it. Every possible configuration of tags works.
But it still won't verify.
package com.codegym.task.task19.task1918;
/*
Introducing tags
Read from the console the name of a file containing HTML.
Example:
Info about Leela <span xml:lang="en" lang="en"><b><span>Leela Turanga
</span></b></span><span>Super</span><span>girl</span>
The main method's first parameter is a tag name. For example, "span".
Display all tags that match the specified tag.
The order should match their order in the file, each tag on a new line.
The number of spaces, newline characters (\n), or carriage returns (\r) does not affect the result.
The file does not have a CDATA tag. Each opening tag has a separate closing tag, and there are no self-closing tags.
Tags may have nested tags.
Example output:
<span xml:lang="en" lang="en"><b><span>Leela Turanga</span></b></span>
<span>Leela Turanga</span>
<span>Super</span>
<span>girl</span>
Tag templates:
<tag>text1</tag>
<tag text2>text1</tag>
<tag
text2>text1</tag>
text1 and text2 can be empty
Requirements:
1. The program must read the file name from the console (use BufferedReader).
2. The BufferedReader used for reading input from the console must be closed after use.
3. The program must read the file's contents (use FileReader).
4. The file input stream (FileReader) must be closed.
5. The program must write to the console all the tags that match the tag specified by the argument passed to the main method.
text = 'I want to find a string between two substrings'
left = 'find a '
right = 'between two'
# Output: 'string'
print(text[text.index(left)+len(left):text.index(right)])
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.*;
public class Solution {
public static boolean debug = false;
public static void main(String[] args) throws Exception {
BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
String fileName = !debug ? re.readLine() :
"C:\\Users\\Alfred\\Documents\\CodeGym Data Files for exercizes\\data.txt";
re.close();
StringBuilder bufBuilder = new StringBuilder();
BufferedReader fileread = new BufferedReader(new FileReader(fileName));
while (fileread.ready()) {
bufBuilder.append(fileread.readLine());
}
fileread.close();
String data = bufBuilder.toString().replaceAll("[\n\r]", "");
// Create Tags for Matching
StringBuilder otagBuild = new StringBuilder().append("<" + args[0]);
String oTag = otagBuild.toString();
StringBuilder ctagBuild = new StringBuilder().append("</" + args[0]);
String cTag = ctagBuild.toString();
// Create ordered TreeMap of tags and locations
int locT = 0;
TreeMap<Integer,String> tagTree = new TreeMap<>();
while (locT < data.length()) { // locate all Open tags
locT = data.indexOf(oTag, locT + 1);
if (locT == -1) break;
tagTree.put(locT,"O");
}
while (locT < data.length()) { // locate all Close tags
locT = data.indexOf(cTag, locT + 1);
if (locT == -1) break;
tagTree.put(locT,"C");
}
// Create an indexable ArrayList of tag and location pairs Tags objects
ArrayList<Tags> tagLst = new ArrayList<>();
for (Map.Entry<Integer,String> entry : tagTree.entrySet()) {
tagLst.add(new Tags(entry.getKey(),entry.getValue()));
}
// Print them out to see
if(debug) {
for (Tags t : tagLst) {
System.out.print(t.key + " : " + t.val + " | ");
}
System.out.println();
}
// Begin Tag sorting and printing Process //
int counter = 0;
for (int i = 0; i < tagLst.size(); i++){ // find next set
if (tagLst.get(i).val.equals("O")) { // if tag is an open tag the begin search for close tag
for (int j = i + 1; j < tagLst.size(); j++) { // look for Close tag / new j
if(debug) {
System.out.println("i = " + i + " j = " + j + " c + " + counter);
System.out.println("tag at " + j + " is " + tagLst.get(j).val);
System.out.println();
}
if (tagLst.get(j).val.equals("O")) { //if new j is an open tag
counter++; // add 1 to the counter
if(debug) { System.out.println("Counter = "+counter);}
}
if (tagLst.get(j).val.equals("C")) { // if new j is a close tag
if (counter != 0) { // if the counter is not zero
counter--; // subtract 1 from the counter
if(debug) { System.out.println("Counter = "+counter);}
} else {
// if (counter == 0){ // If the counter Is 0 then print the Tag Line
if(debug) { System.out.println("i = "+i+" j = "+j +" c + "+counter);}
System.out.println(data.substring(tagLst.get(i).key, (tagLst.get(j).key) + cTag.length()));
break;
}
}
} //end for j loop
} // end of if next i is Open tag
} //end for i loop go to top and find next set
}
public static class Tags{
int key;
String val;
public Tags(int key,String val){
this.key = key;
this.val = val;
}
}
}
///////////////////////////////////////////////////////////////
/*
if (debug) {
// System.out.println(oTag); // Opening Tag
// System.out.println(cTag); // Closing Tag
// System.out.println(data.indexOf(oTag, 18));
//System.out.println(data.indexOf(cTag, 0));
}
/*
// Create lists of tag locations
ArrayList<Integer> oTags = new ArrayList<>();
ArrayList<Integer> cTags = new ArrayList<>();
int locT = 0;
while (locT < data.length()) {
locT = data.indexOf(oTag, locT + 1);
if (locT == -1) break;
oTags.add(locT);
}
locT = 0;
while (locT < data.length()) {
locT = data.indexOf(cTag, locT + 1);
if (locT == -1) break;
cTags.add(locT);
}
// Print out Lists Contents to see
if (debug) {
//System.out.println(data.length());
// System.out.println(data.charAt(data.length()-1));
System.out.println();
System.out.print("O : ");
for (int t : oTags) {
System.out.print(t + " ");
}
System.out.println();
System.out.print("C : ");
for (int t : cTags) {
System.out.print(t + " ");
}
System.out.println();
System.out.println();
} // end debug prnt
// Create an array tree of loc and tags
TreeMap<Integer,String> tagTree = new TreeMap<>();
for (int i = 0; i < oTags.size(); i++){
tagTree.put(oTags.get(i),"O");
tagTree.put(cTags.get(i),"C");
}
for (Map.Entry<Integer,String> entry : tagTree.entrySet()) {
System.out.println("Key = " + entry.getKey() +
", Value = " + entry.getValue());
}
/////////////////////////////////////
///////////////////////////////////////
////////////////////////////////////
// Logic for double lists
int c = 0;
int m = 0;
for (int i =0; i< oTags.size(); i++) { ///// New i , new oTag
int o = i;
if(debug)System.out.println();
while (i < cTags.size()) {
if (!(oTags.get(o + 1) > cTags.get(c))) {
System.out.println("Not pair, O:" + o + ",C:" + c + ", Open #O " + oTags.get(o) + ", Close #C " + cTags.get(c));
o++;m++;
} else {
System.out.println("Yes Pair, O:" + o + ",C:" + c + ", Open #O " + oTags.get(o) + ", Close #C " + cTags.get(c));
System.out.println();
System.out.println(data.substring(oTags.get(c-m),cTags.get(o)+7));
if (m!=0)m--;
break;
}
}// end while loop
System.out.println();
System.out.println("Finished oTag : "+c);
System.out.println();
} // end for loop
*/