Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use System.out.printf

Tags:

java

I tried to use printf but I have unexpected errors.

What is the fault on this code:

System.out.printf("The date is %d/%d/%d", month,day,year);

I want to print the date and month, day and year are double variables.

like image 916
Vasilis Drosatos Avatar asked Dec 23 '12 03:12

Vasilis Drosatos


Video Answer


1 Answers

As per the Formatter docs, %d is a conversion for an integral value, which wouldn't work for doubles. You'll want to convert them to integers. Why would you represent month, day, and year as floating point numbers, anyway? You'd be much better off using a Date and using an appropriate formatter for date values.

If you must use doubles for those values, you'll want %f instead of %d.

like image 82
Ryan Stewart Avatar answered Sep 19 '22 16:09

Ryan Stewart