Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite and print the values in console in java [duplicate]

Tags:

java

Here is one while loop that will iterate 11 times.

public static void main(String[] args) {    
        int count=0;
        while(count<=10){       
            System.out.print(count);
            count++;
        }   
    }

& the output will be definitely : 012345678910

But I want to display the same output in such a way that every iteration will overwrite the previous value while printing the value on console.

Here's the restriction is : We can not use file.

clearing the console on every iteration can be one of the ways, is there anything left we can do?

like image 450
S. Das Avatar asked Nov 19 '25 09:11

S. Das


1 Answers

You can use \r which returns to the start of a line: System.out.print("\r" + count); should work.

like image 193
helencrump Avatar answered Nov 21 '25 23:11

helencrump



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!