简体   繁体   中英

Input double with dot instead of comma

I've got a input in my program expecting a double:

Scanner in_num = new Scanner(System.in);
double num1 = in_num.nextDouble();

but in the uploading system it throws exception java.util.InputMismatchException and I found out that it's because of the system writing a dot as a decimal point.

Is there a way in Java to take a number with a dot as a decimal point instead of comma?

  1. Switch your Scanner into US Locale:

    Scanner sc = new Scanner(System.in);
    sc.useLocale(Locale.US);
    double d = sc.nextDouble();

  2. Use Double.parseDouble():

    BufferedReader reader = new BufferedReader(new inputStreamReader(System.in));
    double d = Double.parseDouble(reader.readLine());

I found solution, just had to think a little bit. I've just taken a next String and parsed it to Double.

double num1 = Double.parseDouble(in_num.next());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM