You are viewing a single comment's thread. Return to all comments →
System.out.printf("%-15s%03d%n", s1, x);
system.out.print or println i know but printf i can't please explain
the "f" in printf stands for "Format" if I'm not mistaken.
printf is an avanced version of print. It allows you to not only print, but format it aswell. This example alone shows some nifty tricks. Some others would be formatting the decimals.
If you have a decimal of 5.243012 and you only wanted the first 3 places, you can use
System.out.printf("You Decimal Number is %.3f",myDecimal);
%f means print a float/double and use 3 decimal places.the .3 means to print upto the third decimal (Thousandths)
when you pass variables to your printf command, besure to passthem in the order called.
System.out.printf("Hello %s, my name is %s. I am %d years old, and you are %d years old",yourName,myName,myAge,yourAge);
As you go on, you want to stop use System.out.print and move onto uses System.out.printf when you're printing data to the screen.
You won't need to worry about println because adding a \n to your ptintf will add a line whereever the \n is placed.
This comment saved my bacon, and allowed me to fully grasp the concept. Especially the simplification of the last example... btw I had to spell this out for me:
%d = int
%s = String
%f = float