I want to make a program wher the user enters his password in a java program and as he is typing it, the letters turn to stars so as to hide his password (since I'm trying to design a multiplayer game). Is it even possible using java?

Dani AI

Generated

The thread title is misleading (as pointed out) — the real question is how to mask password input in Java. Short answer: yes. Two practical approaches are common and reliable: use a GUI password field (recommended for a multiplayer game) or use the console API for simple command-line programs.

A simple Swing approach uses JPasswordField. It masks input automatically and exposes the password as a char[] (which should be cleared when done):

JPasswordField pf = new JPasswordField(20);
int ok = JOptionPane.showConfirmDialog(null, pf, "Enter password", JOptionPane.OK_CANCEL_OPTION);
if (ok == JOptionPane.OK_OPTION) {
    char[] pwd = pf.getPassword();
    // authenticate using pwd
    Arrays.fill(pwd, '\0'); // clear when finished
}

For console apps, java.io.Console provides readPassword(), but note it disables echo (no characters shown) rather than showing asterisks. Example:

Console console = System.console();
if (console != null) {
    char[] pwd = console.readPassword("Password: ");
    Arrays.fill(pwd, '\0');
} else {
    // many IDE consoles return null; fallback will be unmasked input
}

Showing live asterisks in a cross-platform terminal requires low-level terminal control or a third-party terminal library; it’s more work and less portable than using a GUI field.

Security notes (important for a multiplayer game): never log or keep passwords as Strings, clear char[] after use, always send credentials over TLS, and store only salted hashed passwords (bcrypt/Argon2 or a vetted library) on the server. ’s suggestion to research is sound; the above gives the core, practical options to implement masking correctly.

Recommended Answers

All 2 Replies

How does your thread title in any way relate to your question?

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.