+ 1

String to double

I want the user to enter a random mathematical expression in a string variable and then I want to convert it to double data type. I tried explicit conversion but it didn't work. what to do?

20th Sep 2016, 2:44 PM
Vivek Anand
2 odpowiedzi
+ 1
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner scn = new Scanner(System.in); String x = scn.next(); try{ Double y = Double.valueOf(x); System.out.println(y); } catch (NumberFormatException e) { System.err.println("input only numbers!"); } } }
21st Sep 2016, 11:50 AM
wave
wave - avatar
0
thanks I'll try that
21st Sep 2016, 2:08 PM
Vivek Anand