5

Say I am making a command-line interface. I want to print out a string, and then change it. For example, when you run the program, it prints out

Hello, World!

A few seconds later, though, the message gets changed to:

Hello, Computer User!

Is this possible? Cross-OS would be preferable.

Piccolo
  • 1,612
  • 4
  • 22
  • 38
  • 1
    Look up "curses" - see http://stackoverflow.com/questions/439799/whats-a-good-java-curses-like-library-for-terminal-applications –  Mar 07 '13 at 06:22
  • 1
    @pst - curses isn't exactly cross-OS. – Ted Hopp Mar 07 '13 at 06:22
  • @TedHopp But an equivalent Java implementation is (or is as much as can be expected) - the link I added was found via `[java] curses` quite trivially. –  Mar 07 '13 at 06:23

2 Answers2

5

You could echo the \b escape sequence, which is backspace, and then write out a new message, in place of the old-one.

Do note, this might not be completely cross-platform, as it is upto the console itself and how it interprets \b, but it should work mostly.

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
3

try

    String s1 = "Hello, word!";
    System.out.print(s1);
    Thread.sleep(1000);
    for(int i = 0; i < s1.length(); i++) {
        System.out.print('\b');
    }
    System.out.print("Hello, computer user");

note that it does not work from Eclipse, run it from command line

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275