I am a newcommer to java programming. i wrote this which compiles without error. when i put an int value like "5" it runs without any problem but when i puts a double value like "5.1" i get error. What is the problem?
package javaapplication1;
import java.util.Scanner; // Scanner is in the Java.Util package
public class JavaApplication1 {
// Your program begins with a call to main().
public static void main(String args[]) {
// create a scanner object
Scanner input = new Scanner(System.in);
// prompt the user to enter a radius
System.out.print("Enter a number for radius: ");
double radius = input.nextDouble();
// Computer area
double area = radius * radius * 3.14159;
// Display result
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Error:
run:
Enter a number for radius: 5.1
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:19)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)