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("Неверный формат строки!"); } } 

}

  • No It will not work - Serhii Dikobrazko
  • one
    Seriously? Do you think that if you assign a string with an expression to a variable that has the type of expected result, will it be calculated? Oh. - Sergey Gornostaev
  • Sergey, how then? If, for example, a user enters a mathematical expression from the keyboard, this is also a string, but it somehow leads to a numerical type - Nikolay Semenov
  • one
    ru.stackoverflow.com/questions/479462/… It shows how from the line - expressions make a double answer - Alex Tremasov
  • thank you very much! - Nikolay Semenov

0