File contents:
Info about Leela Leela Turanga
Supergirl
Output:
Leela Turanga
Leela Turanga
Super
girl
Process finished with exit code 0
package com.codegym.task.task19.task1918;
/*
Introducing tags
C:\Users\Администратор\CodeGymTasks\2.JavaCore\src\com\codegym\task\task19\file18.txt
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Solution {
public static void main(String[] args) throws IOException {
StringBuilder sb = new StringBuilder();
try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedReader fileReader = new BufferedReader(new FileReader(reader.readLine())))
{
while (fileReader.ready()) sb.append(fileReader.readLine());
}
String data = sb.toString();
List<Integer> startIndex = new ArrayList<>();
Pattern startTag = Pattern.compile("<" + args[0]);
Matcher matcher1 = startTag.matcher(data);
while (matcher1.find())
startIndex.add(matcher1.start());
List<Integer> endIndex = new ArrayList<>();
Pattern endTag = Pattern.compile("</" + args[0] + ">");
Matcher matcher2 = endTag.matcher(data);
while (matcher2.find())
endIndex.add(matcher2.end());
while (startIndex.size() > 1) {
for (int i = 0; i < startIndex.size() - 1; i++) {
if ((endIndex.get(i) - 1) < startIndex.get(i + 1)) {
System.out.println(data.substring(startIndex.get(0), endIndex.get(i)));
startIndex.remove(0);
endIndex.remove(i);
break;
}
}
if (startIndex.size() == 1) {
System.out.println(data.substring(startIndex.get(0), endIndex.get(0)));
startIndex.remove(0);
endIndex.remove(0);
}
}
}
}
/*
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.
*Количество пробелов, символов новой строки (\n) или возврата каретки (\r) не влияет на результат.
The file does not have a CDATA tag. Each opening tag has a separate closing tag, and there are no
self-closing tags. - Файл не имеет тега CDATA. Каждый открывающий тег имеет отдельный закрывающий тег,
а самозакрывающихся тегов нет.
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.
*/