0

Is there a way to read input from the keyboard using command line without that input being displayed to the command line using java?

example i want the user to be able to type in random chars and then the system can process these chars without them begin displayed to the screen like a scanner or System.console().readLine; would.

Edit:

  1. I need to read the raw input as chars, not something that requires the user to press enter after their input string

2.similar to what this code does in Haskell

hSetBuffering stdout NoBuffering
hSetBuffering stdin NoBuffering
hSetEcho stdin False
)
  • http://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it – jalopaba Mar 23 '15 at 10:26
  • 1
    Sorry to ask, but you are the third person i've seen this past week asking something like this and none ever explained why they want this. Just curious. – WonderWorld Mar 23 '15 at 10:26
  • 1
    I can't speak for the others who have posted about this as I didn't know anyone else has had this problem, but I'm writing a simple game that takes the users input via one thread and using another the computer generates random chars at decreasing intervals to a buffer,for the user to guess, if they match up the user gets a point, once 10 chars are in the buffer the game stops. Initially it was the computer generating both the buffer chars and the guesses but now i want to modify the code so the user can do input, i have done this in haskell but was wondering if there is a similar way in java. – user4301818 Mar 23 '15 at 10:32

2 Answers2

0

Yes, use java.io.Console.readPassword().

Jesper
  • 186,095
  • 42
  • 296
  • 332
  • wouldn't that require the user to press enter after the input? Sorry i should have said i need raw input – user4301818 Mar 23 '15 at 10:22
  • Yes, if you don't want that, you could write a GUI application instead of a command-line application. There's no way to read individual characters from the console without echoing them as far as I know. – Jesper Mar 23 '15 at 10:24
0

No I don't think that there is a way to do this with the command-line. But instead you could use a JFrame and add a CaretListener to it.

nom
  • 246
  • 3
  • 14