The Wayback Machine - http://web.archive.org/web/20220614064606/https://www.geeksforgeeks.org/java-io-console-class-java/amp/

Java.io.Console class in Java

The Java.io.Console class provides methods to access the character-based console device, if any, associated with the current Java virtual machine. The Console class was added to java.io by JDK 6.

Important Points:

Important Methods:

Program:




// Java Program to demonstrate Console Methods
  
import java.io.*;
class ConsoleDemo 
{
    public static void main(String args[]) 
    {
        String str;
          
        //Obtaining a reference to the console.
        Console con = System.console();
          
        // Checking If there is no console available, then exit.
        if(con == null) 
        {
            System.out.print("No console available");
            return;
        }
          
        // Read a string and then display it.
        str = con.readLine("Enter your name: ");
        con.printf("Here is your name: %s\n", str);
  
        //to read password and then display it
        System.out.println("Enter the password: ");
        char[] ch=con.readPassword();
  
        //converting char array into string
        String pass = String.valueOf(ch);
        System.out.println("Password is: " + pass);
    }
}

Output:

Enter your name: Nishant Sharma
Here is your name: Nishant Sharma
Enter the password: 
Password is: dada

Note: System.console() returns null in an online IDE

This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




Article Tags :
Practice Tags :