9

I'm new to Java and as an attempt to learn more I tried making a clock. It works perfectly, except for the fact that it prints on a new line every time a second changes. How do I make it so that I can replace the text that's already been printed out with the new time?

public class test {
    public static void main(String[] args) {
        test.clock();
    }
    public static void clock() {
        int sec = 0;
        int min = 0;
        int h = 0;
        for(;;) {
            sec++;
            if(sec == 60) {
                min++;
                sec = 0;
            } else if(min == 60) {
                h++;
                min = 0;
            } else if(h == 24) {
                h = 0;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if(h < 10) {
                System.out.print("0"+h+":");
            } else {
                System.out.print(h+":");
            }
            if(min < 10) {
                System.out.print("0"+min+":");
            } else {
                System.out.print(min+":");
            }
            if(sec < 10) {
                System.out.println("0"+sec);
            } else {
                System.out.println(sec);
            }
        }       
    }
}

Here's my code. And here's an example of what I'm trying to do: I want to turn this: 00:00:00, into this: 00:00:01 (the code does this but it prints on a new line and doesn't delete the old time). The thing is that I want to get rid of the first one (00:00:00) and on the same line print the second one (00:00:01).

Is this possible in Java?

0xCursor
  • 2,242
  • 4
  • 15
  • 33
Hugo Schongin
  • 93
  • 1
  • 1
  • 3
  • Related: http://stackoverflow.com/questions/606086/how-do-you-clear-the-java-console – Gnoupi Aug 02 '12 at 12:52
  • See also [What's a good Java, curses-like, library for terminal applications?](http://stackoverflow.com/questions/439799) – McDowell Aug 02 '12 at 12:56

2 Answers2

21

That has nothing to do with java actually. Rather you want the console you are displaying in to overwrite the text.

This can be done (in any language) by sending a carriage return (\r in Java) instead of a linefeed (\n in Java). So you never use println(), only print() if you want to do that. If you add a print("\r") before the very first print it should do what you want.

BTW it would be far more elegant and simple if you built the entire time in a string and then output it all at once :)

Durandal
  • 19,919
  • 4
  • 36
  • 70
  • btw, can eclipse do like that? It isn't working when i test it. – Hugo Schongin Aug 02 '12 at 13:08
  • No the eclipse console does not support that. If you run your class from command line (using java command), it should work as expected (The Windows console supports it, and any Unix console should too). – Durandal Aug 02 '12 at 13:11
  • ok, thank you! Do you know if there's any other compiler i can get that suports it? It takes too long trying things if I were to compile in cmd and then run it every time i change a letter. – Hugo Schongin Aug 02 '12 at 13:15
  • No, I've been using Eclipse for years and haven't tried one of the others since (they all offer more or less the same anyway). The console in an IDE is for debugging purposes, so I presume it would be not to helpfull if they supported carriage returns because it could obscure the output. A simple workaround would be to create a .cmd file (on windows) that contains the java command and after that the pause command. You can then place a shortcut to that on the desktop and it does all the work. Not the most comfortable, but a little better than command line? – Durandal Aug 02 '12 at 13:19
1

you can do System.out.print("\u001B[A"); make cursor go one line up. Here is how it works:

\u001B is escape character (27 ascii code) [ is after escape character the letter means what we want to do with cursor

A: cursor go up, B: cursor go down, C: cursor go right, D: cursor go left, H: go to left upper corner, M delete current line, T: scroll up, S: scroll down, J: remove all lines down to cursor

Warning this don't works on cmd or powershell.

TANDEX
  • 131
  • 1
  • 6