Хотя, с человеческой точки зрения, всё верно
package com.javarush.task.task19.task1914;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
/*
Решаем пример
*/
public class Solution {
public static TestString testString = new TestString();
public static void main(String[] args) throws ScriptException {
PrintStream backup = System.out;
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
PrintStream pStream = new PrintStream(oStream);
System.setOut(pStream);
testString.printSomething();
String expression = oStream.toString().replaceAll("\\n", "");
ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
Object expressionResult = engine.eval(expression.replace("=", ""));
System.setOut(backup);
System.out.println(expression + expressionResult);
}
public static class TestString {
public void printSomething() {
System.out.println("3 + 6 = ");
}
}
}
изобретая велосипеднаписав свой парсер строки, который будет вычленять операнды и оператор или всё-таки можно как-то вразумить валидатор схавать решение черезeval
?