Java Programming
Console for input and output
Print output
Two methods of the System.out object
Method | Description |
---|---|
println(data) | Prints the data argument followed by a new line character to the console. |
print(data) | Prints the data to the console without starting a new line. |
Example 1: The println method
System.out.println("Welcome to the Invoice Total Calculator");System.out.println("Total: " + total);
System.out.println(message);
System.out.println(); // print a blank line
Example 2: The print method
System.out.print("Total: ");System.out.print(total);
System.out.print("\n");
Example 3: An application that prints data to the console
public class InvoiceApp { public static void main(String[] args) { // set and calculate the numeric values double subtotal = 100; // set subtotal to 100 double discountPercent = .2; // set discountPercent to 20% double discountAmount = subtotal * discountPercent; double invoiceTotal = subtotal - discountAmount; // print the data to the console System.out.println("Welcome to the Invoice Total Calculator"); System.out.println(); System.out.println("Subtotal: " + subtotal); System.out.println("Discount percent: " + discountPercent); System.out.println("Discount amount: " + discountAmount); System.out.println("Total: " + invoiceTotal); System.out.println(); } }