how does nextDouble(); scan integer values?

Viewed 31

I'm new to coding and I wrote this code using nextDouble();, it reads both double and integer values. Is this right? How does that happen? This is my code,

    import java.util.*;
class Example{
   public static void main(String args[]){
      Scanner input=new Scanner(System.in);
      double gram;
      double ounce;
      System.out.println("Input ounce:");
      ounce=input.nextDouble();
      System.out.println("ounce in gram:");
      gram=ounce*28.3495;
      System.out.println(gram);
   }
}
1 Answers

The Scanner is using ',' for it's next.Double(). You can simply change this by adding the Countries locale.

input.useLocale(Locale.US);

This should be placed before you set the value of ounce:

input.useLocale(Locale.US);
ounce = input.nextDouble();
Related