In most cases, you can output a carriage return (\r
) rather than a newline (\n
). This depends on the terminal supporting it, which most do. \r
moves the cursor back to the beginning of the line.
EDIT: Obviously, when doing this, use System.out.print
rather than System.out.println
(or just generally use the plain, not ln
, form of the output method you're using) -- since the ln
suffix means that your text is automatically followed with a newline.
Example:
for (n = 10000; n < 10010; ++n) {
System.out.print(String.valueOf(n) + "\r");
}
System.out.println("Done");
When this finishes, you'll probably have this on your console screen:
Done0
...since "Done" is shorter than the longest previous thing you output, and so didn't completely overwrite it (hence the 0 at the end, left over from "10010"). So the lesson is: Keep track of the longest thing you write and overwrite it with spaces.