В чем проблема: reader бесконечно читает какие-то квадраты, хотя строка уже кончилась. Как это исправить? И да, перенос строки не убирается (почему-то, сам не знаю. Смотрел через дебаг). Конечно, для задачи можно поставить "break" при определенной длине результата, но это костыль... Выручайте
public class Solution {
public static TestString testString = new TestString();
public static void main(String[] args) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(outputStream);
PrintStream old = System.out;
System.setOut(stream);
testString.printSomething();
String str = outputStream.toString();
str.replaceAll("\n|\r\n", " ");
StringReader reader = new StringReader(str);
String res = "";
while(reader.ready()) {
String now = "";
now += (char)reader.read();
if (now.equals("0")) {
res += "0";
}
if (now.equals("1")) {
res += "1";
}
if (now.equals("2")) {
res += "2";
}
if (now.equals("3")) {
res += "3";
}
if (now.equals("4")) {
res += "4";
}
if (now.equals("5")) {
res += "5";
}
if (now.equals("6")) {
res += "6";
}
if (now.equals("7")) {
res += "7";
}
if (now.equals("8")) {
res += "8";
}
if (now.equals("9")) {
res += "9";
}
}
System.setOut(old);
System.out.println(res);
}
public static class TestString {
public void printSomething() {
System.out.println("it's 1 a 23 text 4 f5-6or7 tes8ting");
}
}
}