How to delete a character written by console in Java?

11

Is there a way to delete a character written by console with System.out or System.err ?

For example, if I have a process that loads for a while, simulate an animation with the 3 ellipsis ... . Something similar to this demo on fiddle .

int puntos = 0;
final String PUNTO = ".";
System.out.print("Conectando, espera");

while (condicionQueSeCumplaAlAcabarElProceso) {
    if (puntos < 3) {                       // comprobar cuantos puntos hay
        System.out.print(PUNTO);            // poner un punto
        puntos ++;                          // indicarlo
        try {
            Thread.sleep(1000);             // esperar 1 segundo
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    } else {
        // aqui hay que borrar los 3 puntos ¿como se hace?
        puntos = 0;                         // resetear
    }
}
    
asked by Jordi Castilla 15.12.2015 в 17:33
source

2 answers

12

What you are looking for is the \b character that is the retroceso or backspace .

Using it to erase the three points ... as your example, it would be:

int puntos = 0;
final String PUNTO = ".";
System.out.print("Conectando, espera");

while (condicionQueSeCumplaAlAcabarElProceso) {
    if (puntos < 3) {                       // comprobar cuantos puntos hay
        System.out.print(PUNTO);            // poner un punto
        puntos ++;                          // indicarlo
        try {
            Thread.sleep(1000);             // esperar 1 segundo
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    } else {
        // aqui hay que borrar los 3 puntos ¿como se hace?
        puntos = 0;                         // resetear
        System.out.print('\b\b\b');
    }
}

An example of how to use it with numbers to represent the percentage would be:

public static void main(String[] args) throws Exception {
    System.out.print("Progreso: ");
    for (int percentage = 0; percentage < 100; percentage++) {
        System.out.print(percentage + "%");
        Thread.sleep(1000); //Simulamos que algo tardado pasa acá
        int length = String.valueOf(percentage).length() + 1;
        while (length-- > 0) {
            System.out.print('\b');
        }
    }
    System.out.println("Proceso finalizado!");
}

Important: The console must support the \b character. Some consoles (such as the IDE) do not support it by default.

    
answered by 15.12.2015 / 18:35
source
1

This is an example of what you want, based on what our friend Gepser shares, it is important to use \b for the elimination of the character " . ":

        System.out.println("Inicia proceso.");
        System.out.print("Conectando, espera");
        int puntos = 0;
        final String PUNTO = ".";
        int counter = 0;
        while (counter < 100) {
            if (puntos < 3) {                       
                System.out.print(PUNTO);            
                puntos ++;                          
                try {
                    Thread.sleep(500);             
                } catch(InterruptedException ex) {
                    ex.printStackTrace();
                }
            } else {
                puntos = 0;                         
                System.out.print("\b\b\b   \b\b\b");
                System.out.flush();
            }
            counter++;
        }
        System.out.println("\nTermina proceso!");

Print the points one by one and then delete them to start the sequence, appendix demo here

exit:

Conectando, espera.
Conectando, espera..
Conectando, espera...
Conectando, espera.
Conectando, espera..
Conectando, espera...
...
...
...
Termina proceso!
    
answered by 15.12.2015 в 20:47