0

I was doing a simple test. Trying to find the area and volume of a cylinder. When I put however decimals as an input I get the bellow error:

> 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 Test2.Test2.main(Test2.java:10)

I already listed in the code that I am able to get decimals as an input by using the .nextDouble() method, however, I do not know why that does not work. When I, however, place integers as input the program runs correctly.

package Test2;
import java.util.*;

public class Test2 {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in);
        final double PI = 3.14159;
        
        System.out.print("Enter the radius and the length of a cylinder: ");
        double radius = input.nextDouble();
        double length = input.nextDouble();
        
        double area = radius * radius * PI;
        double volume = area * length;
        
        System.out.println("The area is "+ area);
        System.out.println("The volume is "+ volume);
    }
}

I would really appreciate it if someone could help me. Thanks in advance

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • what was your entered data because I checked your code and its working – Mustafa Poya Nov 12 '20 at 12:17
  • This depends on the *locale*, that you have configured. Depending on that, you will have to use `,` or `.` as decimal point for the scanner input. You can make the scanner use another locale via [`Scanner#useLocale(Locale locale)`](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useLocale(java.util.Locale)). – maloomeister Nov 12 '20 at 12:18
  • Why don't you prompt to enter each one? Also, please use `Math.PI` instead of creating your own variable. – Mr. Polywhirl Nov 12 '20 at 12:19
  • It was a mess between the `,` and `.` Thanks for the help. The problem is solved. Basically, all I had to do is add a normal comma `,`. – codebydean Nov 12 '20 at 12:26

0 Answers0