How to modify an existing System.out.println

Asked

Viewed 186 times

0

If I run this code

public class main 
{
    public static void main(String[] args)
    {
        teste(20);
    }

    public static void teste(int maximo)
    {
        for(int i = 0; i < maximo; i++)
        {
            System.out.println(i + " de " + maximo);    
        }
    }
}

he’s gonna print that

0 de 30
1 de 30
2 de 30
3 de 30
4 de 30
5 de 30
6 de 30
7 de 30
8 de 30
9 de 30
10 de 30
11 de 30
12 de 30
13 de 30
14 de 30
15 de 30
16 de 30
17 de 30
18 de 30
19 de 30
20 de 30

but I would like instead of the line repeating itself 20 times just modify the first.

If you could give me an API that can run this would also help.

  • Your question was confused. You want only the first line message to be different, and the rest to remain 'x of N', is that it? If it’s not, explain it better, please.

  • If I understand correctly you want to clean the screen, or show only the last result?

2 answers

5

Use System.out.print() and the escape character \r at the end of the text:

public static void teste(int maximo)
{
    for(int i = 0; i < maximo; i++)
    {
        System.out.print(i + " de " + maximo + "\r");    
    }
}

Instead of System.out.println(), who makes line change after the display, System.out.print() always print on the same line.
The character of escape \r causes the cursor to return to the beginning of the line.

The escape character is ignored by console of Eclipse, will therefore have to execute the program in a command window windows.

The way the code is you won’t be able to see anything, since the window will be opened and closed almost immediately.
To test enter the following changes:

public class App 
{
    public static void main(String[] args)
    {
        teste(4);
    }
    public static void teste(int maximo)
    {
        for(int i = 0; i < maximo; i++)
        {
            System.out.print(i + " de " + maximo + "\r");    
            try {
                //Espera meio segundo antes de prosseguir
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("\r\n");
        Scanner keyboard = new Scanner(System.in);
        //Espera que uma tecla seja premida antes de sair do programa
        String tecla = keyboard.next();     
    }
}
  • Continue printing the same result.

  • 2

    eclipse had an old bug, the console does not support https://bugs.eclipse.org/bugs/show_bug.cgi?id=76936, seems to have been solved in the last releases, or in a release candidate. Anyway if you run the java application via commandline the r will work normally

  • @Reginaldosoares Would now ask the AP where it was executing the code, whether in the IDE or via command line.

  • I’m running on the ECLIPSE Console

  • As @Reginaldosoares said on console of Eclipse doesn’t work.

1

If the intention is to show only the last result, then you can set a variable and run the System.out.print in the end:

private string resultadoFinal:

...

public static void teste(int maximo)
{
    for(int i = 0; i < maximo; i++)
    {
           resultadoFinal = i + " de " + maximo;
    }
}

System.out.print(resultadoFinal);

However if the execution is not within a loop and this code is illustrative only and still assuming the output sent or output will be asynchronous, so it’s like @Ramaral said, Eclipse won’t work using \r

  • I want to make this change just by aesthetic, I ran on CMD with " r" and it didn’t work the same way.

  • 2

    @Lucascarezia Works. Check if you are using System.out.print() and not System.out.println().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.