Search code examples
javanewlineconsole-applicationuser-inputcarriage-return

Java User Input without printing a newline


How, in Java, can I accept user input without printing a newline. I want to give a prompt and reset the cursor with a carriage return if the input is invalid. Console.readLine Scanner.nextInt and BufferedReader.read all seem to print a newline when enter is pressed.

I want the cursor to stay put when the user presses enter to submit input.

Thanks.


Solution

  • You'll have to repaint the whole screen, or use a curses library.

    Sorry. I know that's not what you wanted to hear, but them's the breaks... It's a limitation of Consoles generally, which were designed to allow "dumb terminals" to communicate with a "smart server", and I/O was typically done on a "line by line" basis.

    So you see why us modern programmers just love GUI's, HTML, and basically all things non-console... because console applications where complete, utter, total pain-in-the-arse to write, and they looked like shazbut too ;-)

    Cheers. Keith.