+ 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?
2 Réponses
+ 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!");
}
}
}
0
thanks I'll try that