Why I have got inputMismatchException?
When the inputs are only integer the result will be good, But when I use double inputs the compiler throw an inputMismatchException.
public static void main(String[] args) {
// TODO Auto-generated method stub
// Help by Sahil Muthoo
System.out.println("Enter the radius and length of a cylinder:");
Scanner sc = new Scanner(System.in);
Pattern newlineOrSpace = Pattern.compile(System.getProperty("line.separator") + "|\\s");
sc.useDelimiter(newlineOrSpace);
double radius=sc.nextDouble();
double length=sc.nextDouble();
double area= radius*radius*Math.PI;
double volume=area*length;
System.out.println("The area is " + area);
System.out.println("The volume is " + volume);
}
Forexample inputs: 3 5 it will be good.
Forexample inputs: 3.3 5 it will throw inputMismatchException
You seem to work with the wrong locale. If you do not explicitly tell the JVM which locale to use to interpret the floating point inputs, it will use your system’s (JVM) default locale.
Explicitly set the locale to US format:
I reproduced the problem as follows:
GERMANY(decimal delimiter is comma), input3,5works, but3.5does not.US(decimal delimiter is point), input3.5works, but3,5does not.As this is homework you should go back to your prof and ask for clarification on multi-language requirements 🙂