3

I want to write a program which instantly displays a character when it is typed in console. For example, output asks 'Enter'. Suppose I write char 'g' in console. It should instantly display in console. Also, after entering char, I don't want to press enter. Please explain to me how can I achieve this and also explain the concept.

I have tried this code:

import java.io.*;

public class input {

    public static void main(String[] args) throws IOException {
        InputStreamReader ir=new InputStreamReader(System.in);
        System.out.println(ir.read());
    }
}
mmking
  • 1,278
  • 4
  • 21
  • 33
user3398557
  • 231
  • 1
  • 2
  • 4
  • 4
    Not trivial at all. It requires changing the console to raw mode, as the buffering of input until `Enter` is pressed is done by the operating system. – RealSkeptic Mar 21 '15 at 19:26
  • Look at [this](http://stackoverflow.com/questions/11871520/how-could-i-read-input-from-the-console-using-the-scanner-class) item on SO. – avk Mar 21 '15 at 19:27

2 Answers2

0

Depending on the development environment, System.console() may return null. Personally, I'm not sure what's the safest way to go about instantiating some kind of "mock" console in this case.

Although, you can create the illusion of this by using a native key listener like JNativeHook, which listens for key strokes without a GUI. Once a key is a pressed, you can print to the console using System.out.print. This also ensures that when they user types a key, it's not entered twice (once for the user to enter it, and another time for displaying it). Technically, the console already displays letters as soon as the user types it ;)

Dioxin
  • 13,042
  • 5
  • 35
  • 70
  • You will have the same problem using System.console.... – Aleksander Blomskøld Mar 21 '15 at 19:35
  • @AleksanderBlomskold No you won't. Just because `System.console()` returns null doesn't mean `System.out.println` won't work. Using Eclipse Luna with JDK 8u31, `System.console()` is returning null for me. This doesn't mean I can't print to the console using `println` – Dioxin Mar 21 '15 at 19:36
  • I'm not thinking about the null-problem. System.console().reader().read() also waits for newline before returning. – Aleksander Blomskøld Mar 21 '15 at 19:37
  • @AleksanderBlomskold I'm not suggesting using `System.console()`. I only mentioned that in some environments, based on expierence, it may return null. I'm suggesting using a native key listener to receive input events – Dioxin Mar 21 '15 at 19:39
  • You're the one that brought up `System.console()` - the OP uses `System.in()` directly. – Aleksander Blomskøld Mar 21 '15 at 19:45
  • @AleksanderBlomskold The system's input (and output) stream is different from the console... The console is the user interface, while streams are used to receive data from and send data to that user interface. In the case of displaying data, the console is the priority here. I was only stating that if your program didn't recognize your environment's console (due to possible constraints), you could always give the illusion of what you want. – Dioxin Mar 21 '15 at 19:48
-1
String randomWords;
Scanner kb = new  Scanner(System.in);
System.out.print("Please enter words: ");
randomWords = kb.nextLine();
System.out.println(randomWords);

If you want to continuously get and print text then put the last two lines in a loop.