Advertisement



< Prev
Next >



Console



Console is a physical device with a display screen and a keyboard, for example a personal computer, laptop or an ATM machine. The Console class is used to take input from the user working at the console and display the required information.




Constructor :


We cannot create an object of the Console class by using the new keyword because its constructor has a private access, however, we can get a reference to the Console class by calling a static method of the System class, console().

For example:
Console c= System.console();

Note : If a console is unavailable then calling the System.console() method returns a null reference, otherwise a valid reference to the Console class(which represents a console) is returned. Hence after calling the System.console() method, we must make sure that we have got a valid reference of the Console class.




Methods in Console class


All the following methods with their second parameter as Object...args are taking a var-arg type argument, i.e. from zero to many objects of type Object.

Methods Description
void flush() This method flushes the buffered output to the console.

Console format(String frmtSt, Object... args) This method writes the args to the console using format string, frmtSt.

Console printf(String frmtSt, Object... args) This method writes the args to the console using format string, frmtSt.

Console printf(String frmtSt, Object... args) This method writes the args to the console using format string, frmtSt.

String readLine() This method reads a line of String entered by the user at the console. A line of String ends when user presses Enter.

String readLine(String frmtSt, Object...args) This method writes the args to the console using format string, frmtSt, and then this method reads a line of String entered by the user at ths console.

char[] readPassword() This method reads a password entered by the user without echoing it on the user screen. The input stops when a user presses Enter at the console.

char[] readPassword(String frmtSt, Object...args) This method writes the args to the console using format string, frmtSt, and then it this method reads a password entered by the user without echoing it on the user screen. Input stops when a user presses Enter.




Advertisement




Using the Console class to get user input and display the information.


// Java - Program to display Console class.


import java.io.*;

class A
{
public static void main(String... ar)
{
	//Calling the static console() method of System class, which returns a reference to Console
	Console c = System.console();
	
	char[] pwd;
	c.format("Enter your details - \n");		// Note1
	String str = c.readLine("Enter your name ");	// Note2
	pwd =c.readPassword("Enter your password ");	// Note3
	c.printf("Your details are - \n");
	c.format("Your username is : %s \n", str);	// Note4
	c.printf("Your password is : ");	

	//Displaying password with for-each, one char a time 
	for(char ch : pwd)
	c.printf("%c", ch); 				// Note5
}
}



Output is -:


Enter your details -
Enter your username SkyGazer
Enter your password
Your details are -
Your username is : SkyGazer
Your password is : abcdef


Program Analysis




Argument Type Format String
String %s
char %c
int %d
boolean %b
float %f
Table 1.1




Please share this article -




< Prev
Next >
< PrintWriter
What is Serialization? >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement