i need help ! instead of ouputting
password : admin

i want to input asterisk
password : *******

thanks in advance

Dani AI

Generated

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:

  • System.console() is often null inside IDEs — run your program from a real terminal to use Console.
  • readPassword() returns a char[] (preferred over String); clear it with Arrays.fill when done.
  • There is no simple, portable way in pure Java to echo asterisks live in every terminal; use a library (JLine) or a GUI for that behavior.
  • Never log or print the real password; printing masked asterisks is purely cosmetic.

Recommended Answers

All 3 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.