How many ways to reading data from keyboard in java

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 package.

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

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

  Modified On Nov-20-2017 10:32:25 AM

Leave Comment