Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As an alternative to printing the backspace character, you can use the carriage return character: 13 (decimal), 0xD (hexadecimal), or \r (escaped character).</p> <p>There are three stipulations:</p> <ol> <li>You can't overrun your console width (80 character width is probably a safe assumption)</li> <li>Each iteration you must print as least as many visible characters as the previous iteration.</li> <li>When you want to move on (when you're done with your loading bar or whatever), you need to lead with a newline, or else you'll start overwriting your existing loading bar with new text.</li> </ol> <p>So, something along these lines:</p> <pre><code>public static void loadMyProgram() { while(programStillLoading) { doSomeLoading(); double loadFraction = howCloseToDone(); // A number between 0 and 1 System.out.print("["); int i = 0; for( ; i &lt; (int)(loadFraction * 20); i++) System.out.print("="); for( ; i &lt; 20; i++) System.out.print(" "); System.out.print("] " + (int)(loadFraction * 100) + "%"); System.out.print((char)13); //System.out.print((char)0xD); // Same as above, but in hex //System.out.print("\r"); // Same as above, but as the symbol } System.out.println(); } </code></pre> <p>The above will print <code>[&lt;bar&gt;] &lt;percent&gt;%</code>, where <code>&lt;bar&gt;</code> is <code>0..p</code> '<code>=</code>' followed by <code>0..(20-p)</code> '<code></code>'. It will stay on a single line.</p> <p>Assuming your calculation of the load percentage is monotonically increasing (in other words, it doesn't act like a Windows loading bar, going from 20% to 110% to 1% in the span of 3 seconds), you don't have to worry about ensuring your next output is equal to or longer than your previous output, because that will always be true. However, in this case you could take care of it with two spaces after the '<code>%</code>'.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload