Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

update the console window with java

Tags:

java

console

How can i make java update the current console window instead of going onto a new line (or appending new content onto old).

As an example, if i wanted to demonstrate progress, i would output progress n where n would be the given percentage.

Obviously what i would want to do is simply update n with the current percentage. Any help would be appreciated.

like image 617
richzilla Avatar asked Jan 19 '11 18:01

richzilla


1 Answers

Something like this?

public class Test {   
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            System.out.print("\rThinking... " + i);
            System.out.flush();
            Thread.sleep(100);
        }
    }
}

It's not guaranteed to work on all consoles of course (some IDEs may struggle), but any that obey "carriage return" (the "\r") should be okay.

like image 145
Jon Skeet Avatar answered Oct 04 '22 02:10

Jon Skeet