7.2 Java Console Class
7.2 Java Console Class
The Java Console class is used to get input from console. It provides
methods to read texts and passwords.
If you read password using Console class, it will not be displayed to the
user.
1. String text=System.console().readLine();
2. System.out.println("Text is: "+text);
Method Description
1. Console c=System.console();
Java Console Example
1. import java.io.Console;
2. class ReadStringTest{
3. public static void main(String args[]){
4. Console c=System.console();
5. System.out.println("Enter your name: ");
6. String n=c.readLine();
7. System.out.println("Welcome "+n);
8. }
9. }
Output
1. import java.io.Console;
2. class ReadPasswordTest{
3. public static void main(String args[]){
4. Console c=System.console();
5. System.out.println("Enter password: ");
6. char[] ch=c.readPassword();
7. String pass=String.valueOf(ch);//converting char array into string
8. System.out.println("Password is: "+pass);
9. }
10. }
Output
Enter password:
Password is: 123