1

I’m trying to print a string, only for 1 second.

The first thing I looked for was a method for deleting a line of console - terminal, But don't find anything.

Then try to backspace 5 times for example for “hello” then print another string like “ ” to delete hello string, but I couldn’t find out how \b works for java.

Now I'm confused, how I actually can solve my problem.

Any idea for backspace? Or deleting something that's already printed?

DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42
Sina Abbasi
  • 11
  • 1
  • 2
  • 1
    That's not a problem related to Java but generally on how to delete lines on consoles - and what to do or what is possible thus depends on the console/terminal used. It might not be possible to clear only one line so alternatively you might try to clear and reprint everything except that last line. – Thomas Mar 18 '19 at 16:19
  • @Thomas thanks for your answer. But how can I clear everything? – Sina Abbasi Mar 18 '19 at 17:35
  • How you clear everything depends on the console you're refering to. Windows command line provides the `cls` command for that, Linux has `clear` and other shells might have something else. – Thomas Mar 19 '19 at 08:58

2 Answers2

1

Yor can do it by writing carriage return character \r in equal number of the length of already printed string. It will place the cursor back but will not clear the line but as you will write new characters older will be cleared.

Manishoaham
  • 601
  • 1
  • 5
  • 14
  • 2
    As `\r` sets the cursor to the beginning of the line, why should it matter, how often you print it? – Holger Mar 18 '19 at 17:05
1

I'm not sure to exactly understand what you need, I hope this code will help:

class Main {
  public static void main(String[] args) throws InterruptedException {
    String text = "123456";
    System.out.print(text);
    Thread.sleep(1000); 
    backspace(text.length());
    Thread.sleep(1000);
    System.out.print(text); 
    Thread.sleep(1000);
    arrowLeft(3);
    Thread.sleep(1000);
    System.out.print(text);
  }

  public static void backspace(int number){
    for(int i=0; i<number; i++){
      System.out.print("\b \b");
    }
  }

  public static void arrowLeft(int number){
    for(int i=0; i<number; i++){
      System.out.print("\b");
    }
  }
}
Benfarhat
  • 183
  • 7
  • thanks but my output with your exact code will be: 123456 ???? 123456????123456 . Eclipse cant recognize \b. I don't know why – Sina Abbasi Mar 18 '19 at 17:31
  • @SinaAbbasi look at the [top-rated answer](https://stackoverflow.com/a/7522190/8473028) from the Question that yours is a duplicate of. – DodgyCodeException Mar 18 '19 at 17:56
  • 1
    @Benfarhat yeah your right. Obviously there is something wrong with my eclipse. – Sina Abbasi Mar 18 '19 at 18:57
  • Use terminal or command prompt and see if it works – tgkprog Mar 18 '19 at 20:12
  • 1
    @SinaAbbasi Try to use instead their ASCII equivalent \b -> \010 \t -> \011 \f -> \014 \r -> \015 otherwise, It seems to be a "more than 10 years unresolved [bug](https://bugs.eclipse.org/bugs/show_bug.cgi?id=76936)" with Eclipse IDE – Benfarhat Mar 18 '19 at 20:12