In this code when i write double value in input an error occur

Viewed 98

When I insert a double value in input an error occur such as insert 3.86

public static void main (String args[])
    Scanner reader = new Scanner (System.in);
    double sum = 0.0 , value;
    boolean positivenum = true ; 
    while(positivenum){
        System.out.println("Enter your num or neg to exit");
        value = reader.nextDouble();
        if (value < 0.0)
            positivenum = false;
        else
            sum += value;
        System.out.printf("Your sumtion is " + sum);
    }
}
2 Answers

You need to enter 3,86 instead of 3.86, use , instead of ., or you will get java.util.InputMismatchException.

If you like to use . for double separation you can customize your Scanner like below:

Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);

Now it should work with ..

EDIT: Don't forget to import Locale: import java.util.Locale;

As I already said in my comment, this is because the string is converted according to the current localisation before it gets passed to Double::parseDouble.

You can read that in the JavaDoc of the Scanner::nextDouble method you are using:

[...] by removing all locale specific prefixes, group separators, and localespecific suffixes, then mapping non-ASCII digits into ASCIIdigits via Character.digit, prepending anegative sign (-) if the locale specific negative prefixes and suffixeswere present, and passing the resulting string to Double.parseDouble.

So as I also mentioned in the comment, it'll most likely work when you use a comma as decimal separator. To change this behaviour, you can change the current Localisation.

You can find this information in the JavaDoc of the Scanner class as well:

Localized numbers
An instance of this class is capable of scanning numbers in the standardformats as well as in the formats of the scanner's locale. A scanner's initial locale is the value returned by the Locale.getDefault(Locale.Category.FORMAT) method; it may be changed via the useLocale() method. The reset method will reset the value of thescanner's locale to the initial locale regardless of whether it waspreviously changed.

So what you can do is simply set the Scanners locale to US:

reader.useLocale(Locale.US);

And it will work with "." instead of "," as decimal separator.

As Scanner::useLocale will return the scanner object, you can chain this call and use it in the same line of the construction of the scanner:

Scanner reader = new Scanner(System.in).useLocale(Locale.ROOT);

Since version 1.6 you can also use the ROOT-locale. I would prefer it in this case, because it's meant to be country-neutral, as it's stated in the documentation:

Useful constant for the root locale. The root locale is the locale whoselanguage, country, and variant are empty ("") strings. This is regardedas the base locale of all locales, and is used as the language/countryneutral locale for the locale sensitive operations.

So here's your modified code:

public static void main(String args[]) {
    Scanner reader = new Scanner(System.in);
    reader.useLocale(Locale.ROOT);
    
    double sum = 0d, value;
    boolean positivenum = true;
    while (positivenum) {
        System.out.println("Enter your num or neg to exit");
        value = reader.nextDouble();
        if (value < 0.0)
            positivenum = false;
        else
            sum += value;
        System.out.println("Your sumtion is " + sum);
    }
}

You may need to add an import statement for the Locale-class:

import java.util.Locale;
Related