Валидатор просит проверить рекламный текст, так как в нем опечатка
package com.javarush.task.task19.task1927;
/*
Контекстная реклама
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class Solution {
public static TestString testString = new TestString();
public static void main(String[] args) throws IOException {
PrintStream consoleStream = System.out;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(outputStream);
System.setOut(stream);
testString.printSomething();
String[] text = outputStream.toString().split(System.lineSeparator());
StringBuilder sb = new StringBuilder(outputStream.toString());
String contextAdv = "JavaRush - курсы Java онлайн";
for (int i=0; i < text.length; i++) {
if (i != 0 && i % 2 == 0) {
int index = sb.indexOf(text[i]);
sb.insert(index, contextAdv + System.lineSeparator());
}
}
outputStream.reset();
outputStream.write(sb.toString().getBytes());
System.setOut(consoleStream);
System.out.println(outputStream);
}
public static class TestString {
public void printSomething() {
System.out.println("first");
System.out.println("second");
System.out.println("third");
System.out.println("fourth");
System.out.println("fifth");
}
}
}