adam bien's blog

JDK 1.0+: ASCII Progress Indicator 📎

This example implements a progress indicator as an animated rotating character:

public class Progress {

    private int counter = 0;
    private char sequence[] = {'-','\\','|','/'};

    public void showProgress(){
        counter++;
        int slot = counter % sequence.length;
        char current = sequence[slot];
        System.out.print(current);
        //the backspace character
        System.out.print("\b");
    }    
}

The test below will show a rotating character for 5 seconds. The animation may not work in your IDE or unit test, but should work in the console:


public class ProgressTest {
    @Test
    public void rotate(){
        var progress = new Progress();
        for (int i = 0; i < 20; i++) {
            progress.showProgress();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
            }
        }
    }
}