Knowledge Walls
John Peter
Pune, Maharashtra, India
How to get Password in Core Java with Example
3412 Views
In this example used to get password using readPassword() of java.io.Console interface. System.console() is returning console interface to get the password by readPassword of Console interface.
Example for Getting password on Command Prompt
import java.io.Console;
import java.io.DataInputStream;

class Solution {
    public static void main(String args[])throws Exception{
        System.out.println("Enter the username");
        String username = new DataInputStream(System.in).readLine();
        
        System.out.println("Enter the password");
        Console console = System.console();
        char[] password = console.readPassword();
        
        System.out.println("Username: "+username);
        System.out.println("Password: "+String.valueOf(password));
    }
}
How it works 
Note 

Running on eclipse, System.console() will return NullPointerException.

Previous Topics
Previous lessons of current book.
Computer software engineer articles of One day One Thing to Know
Best Lessons of "One day One Thing to Know"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details