Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Show asterisk for input  RSS feed

 
Mayankk Sinhaa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;
class readpass {
void pass()
{
Console c=System.console();
if(c==null)
{
System.out.println("error");
//System.exit(0);
}
c.printf("testing pass %n");
char Password[]=c.readPassword();
c.printf("Password entered was :%s %n",new String(Password));
}
public static void main(String []a)
{
readpass a1=new readpass();
a1.pass();
}
}




/* the program is working fine if want to get encrypted input....as it shows nothing when any key is pressed in the console .... but i want to print a Astrix (*) when ever a key is pressed ... wat changes can i do to get it wrking.... Thanks in advance... please help me .. */
 
Tony Docherty
Bartender
Posts: 3210
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there is a way to do this via the console and if there is it certainly isn't easy to do.
When you get a password the Console class makes a native call to disable echoing to the console so you can't use that method.
 
Mayankk Sinhaa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:I don't think there is a way to do this via the console and if there is it certainly isn't easy to do.
When you get a password the Console class makes a native call to disable echoing to the console so you can't use that method.



but can anyone here explain a program with Masked User inputs with Astrix(*) , i need it . I am trying my level best but all in vain. please help me guys .
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of trying to work with the native console (which ahs its challenges as Tony noted), I'd use a library like JLine for this (https://github.com/jline). If it doesn't support this out of the box I would imagine that it's not too hard to add that.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!