I am writing a program that first converts the string String str = "cosx/sinx+1";
in the string str = Math.cos(a)/Math.sin(a)+1;
after which it leads to the Double
type, the string conversion is successful, and with the conversion to the Double
type, there are problems
public class Main { public static void main(String[] args) { String str = "cosx/sinx+1"; double a = 2; double b = 5; double n = 10; str = str.replaceAll("sin","Math.sin").replaceAll("cos","Math.cos") .replaceAll("x","(a)"); System.out.println(str); try { Double d2 = Double.valueOf(str); System.out.println(d2); } catch (NumberFormatException e) { System.err.println("Неверный формат строки!"); } }
}