Please help
package com.codegym.task.task19.task1927;
/*
Contextual advertising
*/
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class Solution {
public static TestString testString = new TestString();
public static void main(String[] args) {
PrintStream printStream = System.out;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(byteArrayOutputStream);
System.setOut(stream);
testString.printSomething();
String s = byteArrayOutputStream.toString();
s = insertAd(s);
System.setOut(printStream);
System.out.println(s);
}
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");
}
}
public static String insertAd(String string){
String ad = "CodeGym - online Java courses\r\n";
String[] lines = string.split("\n");
StringBuilder stringBuilder = new StringBuilder();
int lineCount = 0;
for (String s : lines){
stringBuilder.append(s);
stringBuilder.append("\n");
lineCount++;
if (lineCount%2==0)
stringBuilder.append(ad);
}
stringBuilder.deleteCharAt(stringBuilder.length()-1);
stringBuilder.deleteCharAt(stringBuilder.length()-1);
if (lineCount%2==0){
stringBuilder.deleteCharAt(stringBuilder.length()-1);
stringBuilder.deleteCharAt(stringBuilder.length()-1);
}
return stringBuilder.toString();
}
}