Originally Posted by
jashburn
2. Use long type
Your code assumes the speed and time inputs are integers. If you are sticking with this assumption, you can change distanceTravelled from a double to a long. If you do so, you can then use %22d to print out the distance without decimals.
As mentioned above, it's just a matter of having
...
long distanceTraveled;
...
System.out.printf("%1d %22d\n", hour, distanceTraveled);
Everything else remains unchanged. With a
long you don't need (and cannot) specify precision in the format string, and so
%22d can be used. Read through the
Conversions section at
Formatter (Java Platform SE 7 ) on the use of
d and
f conversions.
Remember that
long is a 64-bit integer, whereas
double is a 64-bit floating point number (and therefore has decimal places that requires the use of precision in the format string.) See
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics).