I have been working on small project of mine which has the account name and account password as an array .
After the program runs it ask you to enter your username followed by password then it matches it with its database. If correct: proceed; if not: display login fail.
What I want is when the user enters the password instead of displaying the password as 1234
I want to show it as ****
.
{
Scanner in = new Scanner(System.in);
public int UserNameAndPassword(){
System.out.println("Pleass Enter your username:");
String user =in.nextLine();
System.out.println("Pleass Enter your Password:");
String pin = in.nextLine();
int p= FindNameAndPassword(user,pin);
return p;
}
main
public static void main(String[] args) {
HelpingMethodes h =new HelpingMethodes();
Scanner in =new Scanner(System.in);
while(true)
{
int position,choise;
position = h.UserNameAndPassword();
boolean login = true;
while(position==-1)
{
System.out.println("You entered wrong Username or Password");
System.out.print("Pleas try again\n");
position=h.UserNameAndPassword()
}
Is there any simple way to achieve that in Java?
Consider Console.readPassword. Although it will not mask the password with *, it does the job as it hides the text inputted. Quick code snapshot:
char[] password = System.console().readPassword("Password: ");
System.out.println("Password is: "+ password);
Note: You must run the program via command line. If you're running this through an IDE, you will get null on the Console object.
You'll find a detailed tutorial here.
If you prefer not to use Console, you'd have to implement an alternative solution. You'd have to write a thread that overwrites the input as it's received, which won't be a trivial task. As far as I know, Scanner does not have a built-in masking method.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments