Friday, January 04, 2008

Capturing string input without echo in java

Following code will demonstrate capturing a string without any echo using java:

Create a java file (noEcho.java) with the following code:

import java.lang.System;
import java.io.Console;

public class noEcho {
public static void main(String[] args) throws Exception {
char[] passwd;
Console cons;
if ((cons = System.console()) != null && (passwd = cons.readPassword("[%s]", "Enter Text:")) != null) {
String text = new String(passwd);
System.out.println("You Entered :"+text);
}
}
}


Compile: javac noEcho.java
Run: java -cp . noEcho
Sample output:

[Enter Text:]
You Entered :this is a test string

No comments: