I do not understand, because the produced console output is the same as the defined output.
I have tried with iteration and Stringbuilder but it did not work either, and lot of another methods.
package com.codegym.task.task19.task1913;
/*
Output only digits
*/
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
public class Solution {
public static TestString testString = new TestString();
public static void main(String[] args) {
OutputStream console = System.out;
PrintStream another = new PrintStream(new ByteArrayOutputStream());
System.setOut(another);
System.setOut(new OnlyDigitConsole(console));
testString.printSomething();
}
public static class TestString {
public void printSomething() {
System.out.println("This 1 text 23 is 4 f5-6or7 tes8ting");
}
}
public static class OnlyDigitConsole extends PrintStream {
public OnlyDigitConsole(OutputStream s) {
super(s);
}
@Override
public void println(String x) {
super.println(x.replaceAll("\\D+",""));
}
}
}