Table of Contents

Java Console Output

Java offers rich functionality for printing messages and handling errors.

Java Output

In Java, the System.out.println method is commonly used to print messages to the console. It's akin to displaying a smoothie after blending ingredients—the output is the result of your coded instructions.

Example: Basic Output

Printing Error Messages

Java distinguishes between regular output and error messages through System.out and System.err, respectively.

Example: Error Output

Example: New Line and Quotes


Java Console Input

Java provides several ways to take user input, and one of the most common methods is using the Scanner class from the java.util package.

Importing Scanner Class

To use the Scanner class, we first need to import it from the java.util package at the beginning of our Java program. This is done with the import statement.

import java.util.Scanner;

Creating a Scanner Object

After importing the Scanner class, we can now use it to read various types of input from the console. We create a Scanner object by passing System.in to its constructor. System.in is an InputStream which is connected to the keyboard input.

Scanner sc = new Scanner(System.in);