How to Have User Input Decimals in Java

by Vivek Saxena

The "double" Java data type is a 64-bit floating point type that supports all numbers, including those with decimals. To accept user input decimals in Java, you must first declare a "double" variable, after which you can scan the user-submitted data and then submit that data to the aforementioned variable. The key is to correctly declare the variable, as an improperly configured variable -- string, integer -- will cause Java to automatically truncate the decimal.

1

Declare the variable that will store the user value as a double:

double mileage;

2

Prompt the user to provide the value by using the "println" function:

system.out.println("Please submit your mileage:";

3

Scan the user value into the computer via the "scanner" function:

scanner scanned_value = new scanner(System.in);

4

Pass the scanned value to the previously declared variable by combining the scanner variable with the "nextDouble" function:

mileage = scanned_value.nextdouble();

About the Author

Vivek Saxena has been a full-time freelance writer since 2004, contributing to several online publications. Prior to becoming a writer, Saxena studied computer technology at Purdue University.