Is this the best way to print dynamic percent value? Is there any better way of printing it?

public class PercentPrinter {

/**
 * @param args
 */
public static void main(String[] args) {

    for (int i = 0; i &lt; 100; i++) {
        if (i == 0)
            System.out.print("percent completed: " + i + " %");
        try {
            Thread.sleep(500);
        } catch (Exception e) {
            System.out.println(e);
        }
        if (i &gt; 9 &amp;&amp; i &lt;= 99) {
            System.out.print('\b');
            System.out.print('\b');
            System.out.print('\b');
            System.out.print('\b');

            System.out.print(i + " %");
        } else {
            System.out.print('\b');
            System.out.print('\b');
            System.out.print('\b');

            System.out.print(i + " %");

        }
    }
}
}
Standing on the shoulders of @rolfl…

I would prefer to use a carriage return to return the cursor to the beginning of the line, as it's a bit less fragile than backspacing.

public static void main(String[] args) {
    // Java's Printwriter.format() doesn't tell you how many characters
    // it produced; 30 characters is an overestimate to ensure that
    // the entire "Percent completed: ***%" string gets overwritten.
    final String CLEARLINE_FMT = "\r%30s\r";

    try {
        for (int i = 0; i &lt; 100; i++) {
            System.out.printf("\rPercent completed: %3d%%", i);
            Thread.sleep(500);
        }
        System.out.printf(CLEARLINE_FMT + "Done!\n", "");
    } catch (InterruptedException e) {
        System.out.printf(CLEARLINE_FMT + "Interrupted!\n", "");
    }
}


Or, if you want to print "100%" instead of "Done!":

public static void main(String[] args) {
    // Java's Printwriter.format() doesn't tell you how many characters
    // it produced; 30 characters is an overestimate to ensure that
    // the entire "Percent completed: ***%" string gets overwritten.
    final String CLEARLINE_FMT = "\r%30s\r";

    try {
        for (int i = 0; ; i++) {
            System.out.printf("\rPercent completed: %3d%%", i);
            if (i &gt;= 100) {
                System.out.println();
                break;
            }
            Thread.sleep(500);
        }
    } catch (InterruptedException e) {
        System.out.printf(CLEARLINE_FMT + "Interrupted!\n", "");
    }
}