Conversion of a calculation (String) to double

Asked

Viewed 96 times

0

I have a String conta which is always a calculation (4+4, for example), and I want to pass this value to the resultado which in this case is a double, I’ve tried using the method parse, but I was unsuccessful.

Is there any other way to make this conversion ?

Code:

resultado = Double.parseDouble(conta);
System.out.println(resultado);

Error:

Exception in thread "AWT-Eventqueue-0" java.lang.Numberformatexception: For input string: "4+4"

Remembering that the error is indicating precisely the line I do the conversion.

  • 4

    "4+4" is a string, not a mathematical operation and not a numeral for java. So you won’t be able to convert. If you want to add and convert the result to double, you need to separate the numerical values separately, add and then convert.

  • But the time I convert "4+4" to double would not be the same thing as writing result = 4+4 ?

  • No, as I said, this pro java is a string, it does not know that it is an arithmetic operation.

  • 1
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.