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" 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.
– user28595
But the time I convert "4+4" to double would not be the same thing as writing result = 4+4 ?
– Daniel Santos
No, as I said, this pro java is a string, it does not know that it is an arithmetic operation.
– user28595
Related question How to calculate mathematical expression in a string?
– Francisco