i need help ! instead of ouputting
password : admin
i want to input asterisk
password : *******
thanks in advance
Short version: for a console app use System.console().readPassword(), but note it disables echo (you will NOT see asterisks as you type). If you want visible asterisks while typing you either need a GUI component (JPasswordField) or a third‑party console library (for example JLine). As suggested, drop the BufferedReader approach and use Console when running from a real terminal; ’s current BufferedReader code will echo the password as typed.
Example for a terminal (read without echo, then show a masked line after entry):
import java.io.Console;
import java.util.Arrays;
Console console = System.console();
if (console != null) {
char[] pwd = console.readPassword("Password: ");
StringBuilder mask = new StringBuilder();
for (int i = 0; i < pwd.length; i++) mask.append('*');
System.out.println("Password : " + mask.toString());
Arrays.fill(pwd, ' '); // clear sensitive data
} else {
// running inside an IDE: System.console() is null — run from cmd/terminal or use a fallback
} If you want a simple GUI dialog that shows asterisks as the user types, use Swing’s JPasswordField:
import javax.swing.*;
import java.util.Arrays;
JPasswordField pf = new JPasswordField();
int ok = JOptionPane.showConfirmDialog(null, pf, "Enter Password", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (ok == JOptionPane.OK_OPTION) {
char[] pwd = pf.getPassword();
// use password, then clear
Arrays.fill(pwd, ' ');
} Important notes:
Jump to Post— JamesCherrill 4,733You have to use the
Consoleclass and itsreadPasswordmethods. In that case it's probably easier to drop the BufferedReader and just use Console'sreadLine.
Docs are in the usual place.
import java.io.*;
class finals {
public static void main (String[] args) throws Exception {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String user,pass,length,width,name,choice;
int x,y,z,iw,il,column=1,space,ichoice;
System.out.println("PYRAMID NESTED LOOPS");
for (x=3;x>=0;x--) {
System.out.print("Username :");
user=br.readLine();
System.out.print("Password :");
pass=br.readLine();
}}
//heres the part
this is the part of my program
You have to use the Console class and its readPassword methods. In that case it's probably easier to drop the BufferedReader and just use Console's readLine.
Docs are in the usual place.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.