1

So basically I have input reader that looks like this:

BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
while (true) {
    String userInput = stdIn.readLine();
    if (userInput != null) {
        String message = username + ":" + userInput;
    }
}

When I enter some text in terminal for input let's say:

this is a test

And I hit ENTER it stays there. Is there any way for it to get automatically removed from console WITHOUT clearing the whole console ?

Marijus
  • 4,195
  • 15
  • 52
  • 87

2 Answers2

1

If you want to remove the entire line print '\r' (carriage return)

or if you want to remove part of the output just use '\b' (back space) like this

Community
  • 1
  • 1
Kavin Eswaramoorthy
  • 1,595
  • 11
  • 19
0

No you cannot as you currently treat the console as a line printer. You should however treat it as a terminal that accepts ANSI escape codes. One library that lets you treat the console this way is Lanterna but there are many others. Note that the console needs to recognize the escape codes for this to work (e.g. the normal Eclipse console does not).

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263