question 17

Posted Thursday, September 03, 2009 by SacrosanctBlood in

How do you write a java program that is run on a command prompt and
which displays a progress as:

Status x of 100%

Without going to the nextline. i.e only x keeps changing from 1 to 100
on the same line.



1 comment(s) to... “question 17”

1 comments:

SacrosanctBlood said...

The answer lies in java basics, using, escape characters’.. Specifically
‘\b’(backspace).. Here is code snippet for achieving the desired result.





public static void main(String[] args) {

System.err.print("Status: completed 0 of 100%");

for(int i=1; i<=100; ++i) {

pause();

System.err.print("\b\b\b\b\b\b\b\b\b" + i + " of 100%");

}

System.err.println();

}



static void pause() {

try {

Thread.sleep(1000);

} catch(InterruptedException e) {



}

}



Post a Comment