How to implement data reading from the keyboard without moving the console cursor to the next line using Java? Specifically in the Windows command line.

java.util.Scanner moves the cursor to the next line in its own way, just like java.io.Console .

As far as I know, it's impossible to return the cursor to the previous console line in Java.

 public static String Read() { Scanner Sc = new Scanner(System.in); return Sc.next(); } System.out.print("Turn of player: "); TempPlayerGuess = Integer.parseInt(Read()); System.out.print("."); 

The console issues:

Turn of player: 4
.

But I would like to see:

Turn of player: 4.

  • I don’t know anything out of the box. Maybe this will help: sourceforge.net/projects/javacurses - VladD
  • The course carries Scanner.nextLine (). What about Scanner.next () - kandi
  • @danpetruk Just tried it! It still carries over ( - Destructor
  • Somehow I encountered a similar task while studying java, at that time I had to make a console console emulator and completely control the output, now I’m running a c #;) - FLCL

1 answer 1

In Windows, any reading from the standard input is triggered only after the user has entered the entire string. This is done in order to give the user the opportunity, at least minimally, but to edit the input text. Thus, none of the input methods based on reading from stdin will provide a solution to this problem.

If in Linux you can control the terminal through the escape sequences, then on Windows this trick does not work, you must use the Windows Console API . Unfortunately for you, Java does not provide access to this API - so JNI will help you.

  • Can I return to the previous line? @Pavel Mayorov - Destructor 6:39 pm