# [SOLVED] How to mask user input at output terminal in java?



## Saphala

Hi,

I have searched means and ways to hide user input at output in java terminal but all I found was to use the Console class, which when used I get a null exception point error(Exception in thread "main" java.lang.NullPointerException). My code is below:




Code:


public class Runme extends Staff {
    
        public static void main (String []args) 
        {
            String Password;
            Console console = System.console() ;
            
            System.out.println("Enter password: ");
            char [] password = console.readPassword("Enter password");
             Arrays.fill(password,' ');
         }
     }


Can anybody suggest a way to hide this input in a way that it shows up as "*" at the output or better to hide input itself?

I have Java SE 6 and using Netbeans to execute my program.


----------



## Special2God

*Re: How to mask user input at output terminal in java?*

Your code should work fine, however, you cannot get an instance of Console from within an IDE like netbeans because the Console is only valid for a command prompt window/shell window. 

You need to run your program from command prompt. 
I used the following modified code to test your program:


Code:


public class Test {
	public static void main(String[] args) {
		Console console = System.console() ;

		char [] password = console.readPassword("Enter password: ");
		System.out.println("Password was: " + Arrays.toString(password));
		Arrays.fill(password,' ');
	}
}

Compile this code, then open command prompt and navigate to the parent folder where the .class file containing your compiled source code is stored. 

For example, I have a project called "Programs" with source code stored in .../Programs/src/testPrograms/, and compiled class files stored in .../Programs/bin/testPrograms/.
I navigated to the directory .../Programs/bin/ using command prompt and ran the command "java testPrograms.Test" and the program ran fine (notice that "Test" is the name of my class, the example code in your post is Runme, so your command would be "java testPrograms.Runme"). 

The password will be entered like a Linux password. Asterisks are Not show while you are typing, simply type in some text and press enter, the println() will echo the char array back to you so you can see what you entered.


----------



## Saphala

*Re: How to mask user input at output terminal in java?*

Thanks Special2God.

Now I understand why C is better than any other language!


----------



## Special2God

*Re: How to mask user input at output terminal in java?*

Glad I could help. I personally like Java more than C :wink:
If your question is solved, use the 'thread tools' button (top right of this page) to 'mark this thread a solved'.


----------

