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. Console class is used to take inputs from the user working at a console and display the information.




Constructor :


We cannot create an object of Console class by using new keyword because it's constructor has a private access, however, we can get a reference to Console class by calling a static method of System class, console().
For example -:
Console c= System.console();

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




Methods in Console class


All the methods with second parameter as "Object...args" are taking a var-arg type argument, i.e. from zero to many 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, in frmtSt.
Console printf(String frmtSt, Object... args) This method writes the args to the console using format string, in frmtSt.
Console printf(String frmtSt, Object... args) This method writes the args to the console using format string, in frmtSt.
String readLine() This method reads a line of String entered by the user at ths Console. 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, in 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. Input stops when user presses Enter at the console.
char[] readPassword(String frmtSt, Object...args) This method writes the args to the console using format string, in frmtSt and then it This method reads a password entered by the user without echoing it on the user screen. Input stops when user presses Enter.



Advertisement




Taking the user input and displaying the information, using Console class.


// Program to display Console class.

import java.io.*;

class A
{
public static void main(String... ar)
{
Console c = System.console();
char[] pwd;

c.format("Enter your details - \n");	// Line1

String str = c.readLine("Enter your name ");	// Line2

pwd =c.readPassword("Enter your password ");	// Line3

c.printf("Your details are - \n");

c.format("Your username is : %s \n", str);	// Line4

c.printf("Your password is : ");	

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

}
}



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

PYTHON


Advertisement