Monday, January 09, 2006

Reading a character from the Console

Reading a character from the Console should not be a problem. I hadn't had that problem using C++ back in the college days, but somehow it's not that easy in Java. I have never found the need for it myself, but someone at the local JUG mailing list apparently needs it.

Java has always have the problem of reading a character from the Console, without buffering and echoing back to the Console. This has been an outstanding bug for so long, and it's said to be fixed on Mustang. Well, the bug is really about masking password input, not about un-buffering the character inputs. So, my friend there is not really getting his needs fulfilled.

Someone from the mailing list replied with a link to JCurses. So I tried it. I tried Enigma as well, but it doesn't seem to get the job done.

Here's the code:
import jcurses.system.InputChar;
import jcurses.system.Toolkit;

public class Console {
  public static void main (String[] args) {
    while (true) {
      InputChar c = Toolkit.readCharacter ();
    System.out.println (
        "you typed '" + c.getCharacter() + "' (" + c.getCode() + ")");

      // break on Ctrl-C (3)
      if (c.getCode() == 3) break;

It's quick & simple. Part of another surprise, there are some other bugs that still remained till Tiger release. The list includes not being able to change working directory.

What's the most irritating bug of JDK that has not been fixed, according to you?
Does the voting mechanism for bug fixes really work?

No comments:

Post a Comment