Alessandro Petric
Alessandro Petric

Reputation: 33

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?

Upvotes: 0

Views: 1102

Answers (2)

user
user

Reputation: 33

  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());

Upvotes: 1

Alessandro Petric
Alessandro Petric

Reputation: 33

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());

Upvotes: 0

Related Questions