3

I'm looking to print text on a line that is AFTER where the user is inputing their text.

String userInput;

System.out.println("Hello");
userInput = In.getString();
//I want a System.out.print(""); right here, but I want it to appear while 
//the user is still typing their input.

Print: "Hello"

User is getting input

Print "I am printing this at the same time that the user is typing"

Sammy12333
  • 31
  • 1
  • Main thread cannot do concurrency execution, you need two threads that run concurrently to do this – Ryuzaki L Dec 30 '18 at 04:14
  • 3
    Write a [Java Swing](https://www.javatpoint.com/java-swing) GUI application. Create a text field. Write a Key listener to intercept keystrokes typed into the text field. Create a second text field. For every key detected by the listener, echo the corresponding character into the second text field. Easy Peasy ;) Doing something in line-oriented text mode in Java, without [curses](http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/): not so much... – paulsm4 Dec 30 '18 at 04:15

2 Answers2

1

As mentioned in the comments, keystrokes cannot be captured in raw java console applications.

His could be do e using gui applications like using AWT/Swing GUI.

Since this could be an overkill for your problem, you should know that java suports editing of console outputs in a way.

E.g. if the current output has hello, printing \b character will erase one character from console and it will look like hell.

Perhaps you can think in these lines and build your functionality.

YetAnotherBot
  • 1,937
  • 2
  • 25
  • 32
0

You can put console into raw mode. There is no built-in platform-independent way of getting there. This might be a solution you can implement,

Non blocking console input in Python and Java

got it from here. There are more solutions in there that might be helpful

Sandeepa
  • 3,457
  • 5
  • 25
  • 41