my password program can hide keyboard input . its invisible when i type password.

But my qus is how can show password input as asterisk (*)

can any one help me please? :p

my code is here:

public void login()
    {
        Scanner input = new Scanner(System.in);//create Scanner object

        System.out.println("\n");


        Console console = System.console();

        char[] ad_password = "admin".toCharArray();
        char[] sm_password = "salesman".toCharArray();

        char[] passwordEntered = console.readPassword("Enter Password To Access The Project ( As Admin / Sales Man ): ");

        if (Arrays.equals(ad_password, passwordEntered))
        {
            System.out.println("\n Congratulation!!! Access granted \n");
            System.out.println("\n Welcome Admin. :-) \n\n");

            Shoping_mall obj_ad_dis=new Shoping_mall();

            obj_ad_dis.ad_dis();
        }

        else if(Arrays.equals(sm_password, passwordEntered))
        {
            System.out.println("\n Congratulation!!! Access granted \n");
            System.out.println("\n Welcome Sales Man. :-) \n\n");

            Shoping_mall obj1_sm_dis=new Shoping_mall();
            obj1_sm_dis.sm_dis();
        }

        else
        {
            System.out.println("\n Error: Your Password Doesn't Meet. Access Denied !!! :-( \n\n");
            System.out.println("\n Enter Correct Password.\n\n");

            Shoping_mall obj_login=new Shoping_mall();//creat object for calling Admin_works() method
            obj_login.login();
        }
    }

Recommended Answers

All 7 Replies

on or This looks difficult! I looked at the source code for readPassword and it uses a native method to turn off echoing, plus the Console class is final, ie you can't extend it. I'd move on to something else if I were you.

commented: Yeah, done that before and my conclusion was the same back then ;) +13

Um new in java field. :(
how can it be easy? can you give me a example?

I don't think it can be easy. If it is possible it will be very hard.

ok. :(

in Swing it'll be easy, but afaik Console doesn't have the functionality to deal with this

It is safer if no echo feedback is given at the time you enter your password.
That way if somebody behind you is looking over your shoulder, that person can't tell how long your password is by counting the number of asterisks on your screen.
Perhaps you find this a ridiculous reason, but I think it adds some extra safety.
My advice is to just use the existing functionality from the Console class, even if it doesn't display asterisks.
Does it really bother you to not have asterisks displayed?
One could probably fix it by writing native code, but that will most likely render that code unportable (or more difficult to port).
Think about it: you could spend alot of time trying to fix this but it doesn't really help you alot in my opinion once it is "fixed".
I suggest you that you focus your efforts more towards the core features of your program.

It isn't really that complicated IMO; just use jLine; it works out of the box on Windows (and I believe *nix too)

Try out this simple snippet after adding the JAR to classpath:

public class Test {

    public static void main(final String[] args) throws Exception {
        ConsoleReader reader = new ConsoleReader();
        Character mask = '*';
        String line = null;
        do {
            line = reader.readLine("Enter Password(blank pwd to exit)> ", mask);
            System.out.println("Got password: " + line);
        } while (line != null && line.length() > 0);
    }

}
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.