System Output

System Output 
To output information to the screen in Java we use the system output class called System.out.

System.out.println("This is my output");

The printf command can be used to format your output when printing out. The printf method accepts a string parameter which includes information about how you want the output formatted and then a number of parameters for output separated by commas. The format string parameter uses a % sign to indicate where the variables should be outputted with formatting information put directly after the % sign and ending with the data type.

Data types for printf:
‘s’ – String
‘f’ – float or number with decimal places
‘d’ – integer
‘e’ – floating-point in scientific notation

The example below outputs String, integer and decimal variables. In the formatting string, the % begins the format. The first part shows -5 which means that 5 spaces must be placed after the string. If the ‘-’ sign were removed, the spaces would be placed before the string. The ‘s’ means that the data type is of type String. We then have a %d which means an integer type. The next % has 1.2f after which to output the decimal (‘f’) with space for 1 number before decimal point and 2 decimal places after.

String name = "John";
int number = 5;
double amount = 5.37;

System.out.printf ("%-5s%d %1.2f", name, number, amount);

Output will look like: John 5 5.37