Console in Java

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
By Tarun Luthra
Free
star5
Enrolled: 1000
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
Tarun Luthra
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

To get console input, we use the Java Console class. It offers ways to read text messages and passwords. If we read a password using the Console class, the user won't see it. Because System.in and System.out provides access to the majority of the Console's capabilities, it serves mostly as convenience class. However, when reading strings from the console, its use can make some console interactions simpler. Constructors are not provided by Console. Instead, using System.console() returns a Console object. A reference to the console is returned if one is present. Null is returned if not. In some circumstances, a console won't be accessible. Therefore, no console I/O is possible if null is returned.

Introduction to Console in Java

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console class to the java.io API.

To obtain console input, utilize the 1. It offers ways to read text messages and passwords. The user won't see the1if you read it using the Console class.

Internally, the system console is connected to the java.io.Console class. Since 1.5, the Console class has been available.

Console Class Declaration in Java

public final class Console extends Object implements Flushable

Java Console class extends the Object class which means it inherits all methods and attributes from the Object class, Object class is the superclass of the Console class. Console class implements Flushable Interface.

Different Methods of Console Class in Java

MethodDescription
Reader reader()It is utilized to retrieve the console's related reader object.
String readLine()A single line of text from the console is read using this method.
String readLine(String fmt, Object... args)A formatted prompt is given before the console's single line of text is read.
char[] readPassword()It can read passwords that are not visible on the console.
char[] readPassword(String fmt, Object... args)The formatted prompt is given, and after that, the console reads the password that isn't being displayed.
Console format(String fmt, Object... args)It is used to write a formatted string to the output stream of the console.
Console printf(String format, Object... args)It is used to write a string to the output stream of the console.
PrintWriter writer()The PrintWriter object connected to the console is retrieved using it.
void flush()The console is flushed using it.

Reader reader()

It is used to retrieve the console’s related reader object. In this example, we are going to see how to use reader() method in java

Output

Code Explanation

In the above code, we have used reader() method of the console class to print the reader object associated with the console.

String readLine()

A single line of text from the console is read using this method. In this example, we are going to see how to use readline() method in java.

Output

Code Explanation

In the above code, we have used readline() method of the console class to read a line from the console and stored it in an str variable then print it.

String readLine(String fmt, Object... args)

This method of console class is used to read a single line from the console by providing a formatted prompt.

Parameters *fmt*: A format string for the prompt text.
*args*: It represents the arguments in the string format that are referenced by the format specifiers.

Return

return string that is read from the console return null if the stream is ended.

Exception*

  • IllegalFormatException when a format string contains unsupported format specifiers or illegal syntax for the arguments.
  • IOError: If an I/O error occurs.

In this code, we are going to use readline(String fmt,Object ... args) method of the console class to read a line from the console.

Output

Code Explanation

In the above code, we have given string format as %s %s, and "hello", and "world" are passed as arguments.

char[] readPassword()

It can read passwords that are not visible on the console. Returns returns a character array with the password or passphrase that was read from the console, without any line-termination characters, or null if the stream has come to a stop.

Throws: IOError

In this example, we are going to use readPassword() method of the console class to read passwords from the console.

Output

Code Explanation

In the above code, we have used readPassword() method of the console class to read passwords from the console which will be stored in a char array.

Console Format(String fmt, Object... args)

It is used to write a formatted string to the output stream of the console.

Parameters

fmt: A format string according to the Format string syntax definition.

args that the format specifiers in the format string have referred to.

In this example, we are going to use format() method of the console class to write a formatted string to the output stream of the console.

Output

Code Explanation

In the above code, we have written Items, Quantity, and Price details to the console with proper string format.

Console printf(String format, Object... args)

It is used to write a string to the output stream of the console.

Syntax

Parameter

format - format string as defined in format string syntax

args - Arguments referenced by the format specifiers in the format string.

Returns returns this output stream.

Exception

  • IllegalFormatException: if the format string is invalid.

In this example, we are going to use printf() method of the console class to write to the console.

Output

Code Explanation

In the above code, we have used printf() method to write a formatted string to the console output stream.

Here %d is used for the integer.

PrintWriter writer()

The PrintWriter object connected to the console is retrieved using it.

Parameters: This method doesn't take any parameters.

Return value: Returns the PrintWriter Object associated with the console

Exceptions: This method does not throw any exceptions

In this code, we are going to use writer() method of the console class to retrieve the unique PrintWriter object which is associated with the console.

Output

Code Explanation

In the above code, we have retrieved the unique PrintWriter object which is associated with the console object.

void flush()

The console is flushed using it.

  • This method doesn't take any parameter
  • This method does not return any value
  • This method does not throw any exception

We are going to use flush() method to better understand it.

Code Explanation

In the above code, we have used readline() method to get string input from the user and printed that string. After that, we used flush() method to flush the console.

Ways to Get the Object of Console in Java

The static function console() of the System class returns a singleton instance of the Console class.

Syntax

**Code**

Output

Code Explanation

We have used System.console() to get the object of the console class in java.

Ways to Read Java Console Input

It is increasingly popular as a method of reading user input from the command line. Additionally, the format string syntax (like System.out.printf()) can be used to receive input that resembles a password without echoing the user's input.

Advantages

  • reading a password without repeating the characters that were typed.
  • Reading techniques are coordinated.
  • Syntax for format strings is available.
  • Does not function in a non-interactive environment (such as in an IDE).

Example

Example 1

In this example, we are going to see how to take input from users using console class. **Code**

Output

Code Explanation

In the above example, we used the console class to get the input from the user using the readline method of the console class.

Example 2

In this example, we are going to use readPassword() method of the console class to read passwords from the console

Output

Code Explanation

In the above code, we have used readPassword() method of the console class to read passwords from the console which will be stored in a char array.

Conclusion

  • To get console input, use the Java Console class. It offers ways to read text messages and passwords.
  • If you read a password using the Console class, the user won't see it.
  • Because System.in and System.out provide access to the majority of the Console's capabilities, it serves mostly as a convenience class. However, when reading strings from the console, its use can make some console interactions simpler.
  • Constructors are not provided by Console. Instead, using System.console() returns a Console object
  • A reference to the console is returned if one is present. Null is returned if not. In some circumstances, a console won't be accessible. Therefore, no console I/O is possible if null is returned.