I get inputs from user and read it line by line.Then I'm using split method to tokenize the inputs. Like this:
Scanner input=new Scanner(System.in);
String input1=input.nextLine();
String[] tokens=input1.split(" ");
method1(Double.parseDouble(tokens[0]),Double.parseDouble(tokens[1]));
Here is method1
:
public static void method1 (double a, double b) {
System.out.println(a);
System.out.println(b);
}
When I declare 3.5 and 5.3 output;
3.5
5.3
Here there is no problem but if I declare 3,5 and 5,3 my code giving error in below;
Exception in thread "main" java.lang.NumberFormatException: For input string: "3,5"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
How can I fix this problem?
Using
NumberFormat
you can do something like this:Now you can pass
myNumber
to the method that acceptsdouble
value.When using
Locale.FRANCE
, you tell Java that you writedouble
numbers with,
instead of.
.