When I input integer numbers like 5, 7, 14 etc. everything is ok. But when I enter for example 7.5 I get an error. Where is mistake?
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at Exercise_11.main(Exercise_11.java:8)
Below is code:
import java.util.Scanner;
public class Exercise_11 {
public static void main(String[] args) {
Scanner radius = new Scanner(System.in);
System.out.print("Please input the radius of circle: ");
double r= radius.nextDouble();
double l= 2*Math.PI*r;
double s= Math.PI*r*r;
System.out.println("Length = " + l);
System.out.println("Area = " + s);
}
}
Your system's locale is not supporting dot as decimal separator. Use a locale which supports dot as a separator e.g. Locale.ENGLISH
.
Scanner radius = new Scanner(System.in).useLocale(Locale.ENGLISH);
A sample run:
Please input the radius of circle: 7.5
Length = 47.12388980384689
Area = 176.71458676442586