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)

it is perfectly run on netbeans and give correct output.
I have checked it with my own different values.
So where's the problem..?

Member Avatar for vishnu.khera.5

tux4life is correct. It could be a problem with your Locale settings.

Include in your program:

import java.text.*; // this should be with your import statment for Scanner

DecimalFormatSymbols dfs = new DecimalFormatSymbols();  // you can place this at the end of your program
        System.out.println(dfs.getGroupingSeparator()); // enter '5' to test the output
        System.out.println(dfs.getDecimalSeparator());  // I received the output as , .
                                                        // yours would probably show , ,

You can change the decimal separator, refer to the following link
http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormatSymbols.html#setDecimalSeparator(char)

YEs, thanks for helping. it was the prblem with the local settings. i also runs netbeans. but the problem was with the local settings. we use "5,1" instead of "5.1" when i put "5,1" the program runs as it should.

Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.