0

I am making a timer for my program but I can't seem to figure out how to get it to replace the display in the output each time it counts up. So in output if it went up from 1 to 2 it would replace the 1 with two instead of having it like system.out.print would print it as 1 2 or the 2 next to the 1. I have tried using \r but that just created a new line and \b put weird symbols in my code. Any suggestions?

Here is my code, I would like the system.out to replace itself in the same spot, if it's not possible with doing it my command prompt please give other ways I could achieve my outcome

      static void counter(){

        int delay = 0; // delay for however long
        int period = 1000; // repeat every sec.        
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask()
        {
            public void run()
            {

                SEC++;

                if (SEC==60){
                    SEC = 0;
                    MINUTE++;
                }
                if (MINUTE==60){
                    MINUTE = 0;
                    HOUR++;
                }

                System.out.print(HOUR + ":" + MINUTE + ":" + SEC);



            }
        }, delay, period);
    }      
blazefirer
  • 21
  • 1
  • 4
  • 1
    Without code and background information, including which gui library you're using, your question will be *very* difficult to answer. Please consider creating and posting a valid [mcve] with your question. – Hovercraft Full Of Eels Mar 01 '17 at 23:03
  • Sounds like the OP is not using a GUI app, but rather a command-line program that writes to the console. If that is true, it might not be possible depending on the platform and/of console or shell. – FredK Mar 01 '17 at 23:10
  • You could try using backspace character (DEC:08) in the output text. – boxed__l Mar 01 '17 at 23:14

1 Answers1

0

Depending on your IDE, Console, Shell, or other output method this may not be possible. One solution is to try clearing the console.

See here for advice on that and some struggles with it:

Java: Clear the console

Community
  • 1
  • 1
Chris
  • 2,435
  • 6
  • 26
  • 49