
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - Console readPassword(String fmt, Object... args) method
Description
The Java Console readPassword(String fmt, Object... args) method provides a formatted prompt, then reads a password from the console with echoing disabled.
Declaration
Following is the declaration for java.io.Console.readPassword(String fmt, Object... args) method −
public char[] readPassword(String fmt, Object... args)
Parameters
fmt − A format string described in Format string syntax for the prompt text.
args − Arguments referenced by the format specifiers in the format string.
Return Value
This method returns a character array containing the password read from the console, not including the line termination character, or null if an end of stream has been reached.
Exception
IllegalFormatException − If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions.
IOError − If an I/O error occurs.
Important Note
System.console() may return null in IDEs like Eclipse, NetBeans or IntelliJ. Run these programs from the command line (terminal) for proper execution.
Example - Usage of Console readPassword(String fmt, Object... args) method
The following example shows the usage of Java Console readPassword(String fmt, Object... args) method.
ConsoleDemo.java
package com.tutorialspoint; import java.io.Console; public class ConsoleDemo { public static void main(String[] args) { Console console = null; String name = null; try { // creates a console object console = System.console(); // if console is not null if (console != null) { // read line from the user input name = console.readLine("Enter your name:"); // print username System.out.println("Name is: " + name); // read password into the char array char[] pwd = console.readPassword("Enter your password for %s: ", name); // print password length System.out.println("Password length: "+pwd.length); } } catch(Exception ex) { // if any error occurs ex.printStackTrace(); } } }
Console Interaction
Let us compile and run the above program, this will produce the following result −
Your Name: Master Programmer Name is: Master Programmer Enter Password: Password length: 6
Example - Password Input with a Formatted Prompt
The following example shows the usage of Java Console readPassword(String fmt, Object... args) method.
ConsoleDemo.java
package com.tutorialspoint; import java.io.Console; public class ConsoleDemo { public static void main(String[] args) { Console console = System.console(); if (console == null) { System.out.println("No console available"); return; } // Reading a password with a formatted prompt char[] passwordChars = console.readPassword("Enter your password for %s: ", "User123"); String password = new String(passwordChars); // Convert char array to string // Display a confirmation message (never print passwords in real applications) console.printf("Password for User123 received successfully.\n"); // Clear password from memory for security java.util.Arrays.fill(passwordChars, ' '); } }
Console Interaction
Let us compile and run the above program, this will produce the following result −
Enter your password for User123: Password for User123 received successfully.
Explanation
We use System.console() to obtain the Console instance.
If the console is unavailable (e.g., in an IDE), we print an error message and exit.
We use readPassword(String fmt, Object... args) to print a formatted prompt, inserting "User123" dynamically.
The password is read as a char[] (for better security than a String).
We convert it to a String for demonstration purposes (though in real applications, avoid storing passwords as String).
A confirmation message is displayed without revealing the password.
The password array is cleared using Arrays.fill() to remove sensitive data from memory.
Example - Asking for Admin Credentials
The following example shows the usage of Java Console readPassword(String fmt, Object... args) method.
ConsoleDemo.java
package com.tutorialspoint; import java.io.Console; import java.util.Arrays; public class ConsoleDemo { public static void main(String[] args) { Console console = System.console(); if (console == null) { System.out.println("No console available"); return; } // Prompt for admin username String adminUser = console.readLine("Enter admin username: "); // Prompt for admin password with formatting char[] adminPassword = console.readPassword("Enter password for admin user %s: ", adminUser); // Validate input (for demonstration purposes, we assume a hardcoded password) String correctPassword = "SecureAdmin123"; if (new String(adminPassword).equals(correctPassword)) { console.printf("Access granted, %s!\n", adminUser); } else { console.printf("Access denied!\n"); } // Clear password from memory Arrays.fill(adminPassword, ' '); } }
Console Interaction
Let us compile and run the above program, this will produce the following result −
Enter admin username: Admin Enter password for admin user Admin: Access granted, Admin!
Explanation
The program prompts for an admin username using readLine().
It then prompts for a password, inserting the username dynamically into the message.
The input password is compared to a hardcoded correct password ("SecureAdmin123") for authentication.
The result (access granted or denied) is displayed.
The password array is cleared from memory for security.