5.11.2. Output.


Using print and println

System.out.print(s)

This writes string s to the standard output. Note that s can be any expression that yields a string. For example,
  int numcats = 4;
  System.out.println("I have " + numcats + " cats");
writes
I have 4 cats

You can actually pass print a variety of different types of parameters. For example,

  System.out.print(40);
writes 40.


System.out.println(s)

System.out.print writes exactly what you ask it to write, no more and no less. So
  System.out.print("I");
  System.out.print("am");
  System.out.print("joyful");
writes
Iamjoyful
One way to write an end-of-line character is to include \n in your strings. Another is to use System.out.println(s), which writes string s followed by an end-of-line character.


Using printf and format

An alternative is to use System.out.printf, which is a formatted print. The general form is

  System.out.printf(format, thing1, thing2, …);
where format is the format and the things after it are things to insert into the format. For example,
  int numcats = 12;
  System.out.printf("I have %d cats.\n", numcats);
writes
I have 12 cats.
where the value of numcats has been substituted for %d in the format. Similarly,
  int numcats = 12;
  int numdogs = 2;
  System.out.printf("I have %d cats and %d dogs.\n", numcats, numdogs);
writes
I have 12 cats and 2 dogs.
replacing the first %d by the value of the first parameter after the format and replacing the second %d by the value of the second parameter after the format.

If you want to build a string without writing anything, use System.out.format(…). for example,

  int    numcats = 12;
  int    numdogs = 2;
  String str     = System.out.format("I have %d cats and %d dogs.\n", numcats, numdogs);
makes str be "I have 12 cats and 2 dogs.".

Formats begin with % and tell how to write a value and how to format it, if desired. Formats include the following,

%d

Show the value as an integer in decimal (base 10) form.

%b

Show a boolean value (as true or false).

%s

Show the value as a string. Use this to show characters as well as strings.

%f

Show as a real number in fixed-point format (xxx.xx).

%E

Show as a real number in scientific notation (x.xxxxEnn).

The following formatting indications are available.

%10d

Show an integer in 10 total characters, padding with blanks on the left as necessary. You can use any width, not just 10. This will never shorten a number. So more than 10 characters might be used.

Use this with any format specifier. For example, %30s asks for a string, padded on the left with blanks to make a total of 30 characters.


%-10d

Like %d, but pad with blanks on the right.

%10.2f

Like %f, but use a total of 10 characters, with 2 characters to the right of the decimal point.

%10.5E

Like %E, but use a total of 10 characters, with 5 characters to the right of the decimal point. (There will be one digit to the left of the decimal point.)


Writing to a file

To write to a file, create an object that writes to the file. Assume that you have imported java.io.FileWriter, as in

  import java.io.FileWriter;
Then statement
  FileWriter w = new FileWriter("stuff.txt");
creates an object w that works like System.out, but that writes to file stuff.txt. (If file stuff.txt cannot be written, that statement throws exception java.io.IOException.) For example,
  FileWriter w = new FileWriter("stuff.txt");
  w.println("This will be written into stuff.txt");
writes a line into file stuff.txt.

When you are finished writing to a file, close it. Statement

  w.close();
closes FileWriter w.