The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


imputting characters problem, plz help

Posted by Linda on August 16, 2001 at 2:29 AM

> I need a function similar to getChar() but does not wait for user input. I need a function which gets a character only if a char is inputted. Let me know of any ideas. Thanks in advance.

The InputStreamReader class in Java will read a stream a character at a time, or a line at a time. It works perfectly on files and other proper streams. Stdin however is a special case.

When using stdin as an input source stream, the VM apparently does not get the characters until the OS is finished with them, which is when the user hits enter. Until then, stdin is empty - there are no characters to read until the OS releases them to the VM.

In the graphical environment, the class libraries of which are OS specific and written in C/C++, the keyboard buffer itself is fully accessible. In that context, keyboard actions are always relative to components.

If you want the functionality of getchar() in Java, you have to go native - getchar() works just fine in-line in Java. However, your solution will not be portable.



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us