patternjavaMinor
Is this the best way to print dynamic percent value?
Viewed 0 times
thisthepercentwayvalueprintdynamicbest
Problem
Is there any better way of printing it?
public class PercentPrinter {
/**
* @param args
*/
public static void main(String[] args) {
for (int i = 0; i 9 && i <= 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 + " %");
}
}
}
}Solution
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.
Or, if you want to print "100%" instead of "Done!":
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 < 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 >= 100) {
System.out.println();
break;
}
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.printf(CLEARLINE_FMT + "Interrupted!\n", "");
}
}Code Snippets
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 < 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", "");
}
}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 >= 100) {
System.out.println();
break;
}
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.printf(CLEARLINE_FMT + "Interrupted!\n", "");
}
}Context
StackExchange Code Review Q#40242, answer score: 6
Revisions (0)
No revisions yet.