dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
May 19, 2021

Java: Reading 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;
Java
class ReadingFromConsole
{
	public static void main(String[] args)
	{
		ReadingFromConsole readingFromConsole = new ReadingFromConsole();
		readingFromConsole.proceed();
	}

	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);
	}
}

/*

Output:

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

Please enter password

You entered(password): [C@15db9742
MS Sridhar
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date