Abs said:
Guys, i need a way so that i can mask the password on my program. I
want the password to appear as **** or something like that. I cant use
applets, awt or any other thing beyond our ICSE Std X syllabus. Threads
would be OK i guess but pls try avoiding it. Plz help
How the **** are we supposed to know what's in your syllabus? And you'd get
better help if you put something useful in the subject line.
If you're writing a console app, read a character from the input stream and
print an asterisk to the output stream; repeat until you get an end of
line character. Something like (untested):
public String getPassword( InputStream in, OutputStream out, String prompt)
{
StringBuffer passBuff = new StringBuffer();
boolean done = false;
out.print( prompt);
while ( ! done)
{
int c = in.read();
switch ( c)
{
case -1: /* EOF */
case '\n':
case '\r':
/* and any other characters you see as terminating */
out.println();
done = true;
break;
default:
passBuff.append( ( char)c);
out.print( '*');
break;
}
}
return passBuff.toString();
}
The while loop here may lose you marks for style; you should probably
recode it as a for loop. I used while primarily to make it clearer.