Java: Reading from the Console

Wednesday May 19th 2021 by MS Sridhar
Article Image

Follow along with this quick Java tutorial that teaches you how to read from the console.

Today we walk through reading from the Console using Java. This is an important, yet simple, task. We will be learning how to read normal text and password text, which remains invisible to the Console, but still gets captured for further use in our Java program.

Here is an example of how to read from the Console with Java:


import java.io.Console;
class ReadingFromConsole
	public static void main(String[] args)
		ReadingFromConsole readingFromConsole = new ReadingFromConsole();

	private void proceed()
		Console console = System.console();
		System.out.println("Please enter name");
		String textFromConsole = console.readLine();
		System.out.println("You entered(name): " + textFromConsole);
		System.out.println("\nPlease enter password");
		String passwordFromConsole = console.readPassword().toString();
		System.out.println("You entered(password): " + passwordFromConsole);



$java ReadingFromConsole
Please enter name
Bill gates
You entered(name): Bill gates

Please enter password

You entered(password): [C@15db9742
Mobile Site | Full Site