articles

Java Console Class

Anupam Mishra3712 16-Nov-2015

If you want to read password using Console class, it will not be displayed to the user end.

The Console class is internally attached with system console. Console class is defined in java.io package.

Let’s we take example to read user name and password given by the user end.

import java.io.*;  
class ConsoleClassEx{  
public static void main(String args[]){  
Console c=System.console();
System.out.println("Enter your name: ");  
String n=c.readLine();  
System.out.println("Enter password: ");  
char[] ch=c.readPassword(); //It's not shown  user end
String pass=String.valueOf(ch);//converting char array into string  
System.out.println("Welcome "+n); 
System.out.println("Password is: "+pass);  
}  }   
Java Console Class


Updated 07-Sep-2019

Leave Comment

Comments

Liked By