1

I have a little code that displays a countdown in the console.

for(int i = this.roundTimerMinute; i > -1; i--)
    {
        for(int j = this.roundTimerSeconds; j > 0; j--)
        {

            if( j < 10)
            {
                System.out.println(i + ":0" + j);
            }
            else if(!(i == 0 && j == 60) && (j < 56 && i == 1))
            {
                System.out.println(i + ":" + j);
            }
            else if(i == 0 && j != 60)
            {
                System.out.println(i + ":" + j);
            }

            if(i == 0 && j == 60)
            {
                System.out.println("1:00");
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {}
        }
    }

Currently the output is something like:

1:55
1:54
1:53 
.
.
.

Is is possible to display the whole countdown in only one line. Not consecutively but the new println() overdisplays the old one like this:

1:55 (this display is overridden 1 seconds later by 1:54 and so on)

I would appreciate any help. Thanks

MageD

Edit: So it works with the /r command but only in the CMD of Windows itself. Maybe its a bug or so, but it answers my question. Thank you :)

MageD
  • 13
  • 5

4 Answers4

2

Yes, use print rather than println and add a "\r" at the end of each output:

e.g. System.out.println(i + ":0" + j + "\r");

When you use println it does 2 things: moves onto the next line (line feed) and returns the cursor to the start of the line (carriage return). Think of it like a typewriter :-)

If you manually print "\r" (just the carriage return) without advancing to the next line then the next thing you print will overwrite the previous contents of the line.

mikej
  • 65,295
  • 17
  • 152
  • 131
  • I just tried that. I does not work for me. It still displays each number under each other. Just like with System.out.println() – MageD Apr 09 '16 at 21:15
  • Are you running your code from the command line or from within your editor/IDE? – mikej Apr 09 '16 at 21:19
  • I use Eclipse as IDE. So the code is running in the IDE – MageD Apr 09 '16 at 21:21
  • Ok, it's possible Eclipse's output window isn't handling the `\r` correctly. Are you on a recent Eclipse? This used to be a bug in Eclipse but it was meant to be fixed in the release from June last year. – mikej Apr 09 '16 at 21:26
  • I use Version: Luna Release (4.4.0). I can try this in the cmd console. – MageD Apr 09 '16 at 21:28
  • Cool, give it a go from the command line, or try Eclipse Mars – mikej Apr 09 '16 at 21:31
0

You could try this: Java: Updating text in the command-line without a new line

You can use a "\r" to Carriage Return without using \n which is a New line. The carriage return should put you back at the beginning of the line.

Community
  • 1
  • 1
Someone
  • 560
  • 9
  • 21
0

you want to write something to your terminal (like 1:59), and then erase it and over-write it with something else (like 1:58) ? If so, then no there's no easy way to do that with regular System.out methods. You need to have knowledge of the control characters for your OS and terminal type, or possibly use a library such as one of those suggested here (I've not tried any of them, they're just what Google returned me).

Dan O
  • 6,022
  • 2
  • 32
  • 50
0

You're looking for a Curses library.

Most implementations of curses use a database that can describe the capabilities of thousands of different terminals. There are a few implementations, such as PDCurses, which use specialized device drivers rather than a terminal database. Most implementations use terminfo; some use termcap.

There are a few Java Curses libraries described in What's a good Java, curses-like, library for terminal applications?

It may seem like much, but otherwise you'll need a terminal capabilities database of your own for that one function.

Community
  • 1
  • 1
kervin
  • 11,672
  • 5
  • 42
  • 59