0

I am creating a monitoring application, and I need to print the information to the console, the problem is that each line is added every time I print, I want to know if it is possible to simply update the line and how.

public static void main(String[] args) {
    while (true) {
        System.out.println("Current processes: " + numProcesses);
    }
}
Profeta
  • 13
  • 2
  • Try using 'System.out.print("Current processes: " + numProcesses + "\r")'. "\r" is for carriage return, placing the cursor back at the start of the line. This is how you do it in C but I'm not sure if it works in Java or not. – squidwardsface Nov 23 '21 at 15:50

1 Answers1

1

A new line is being added because you are using the System.out.println method which appends a new line at the end. Using the System.out.print method will not do that. You can use the \r special character to return to the beginning of the same line, which I believe the functionality you're going for.

Something like-

public static void main(String[] args) {
    int numProcesses=0;
    while (true) 
        System.out.print("\rCurrent processes: " + numProcesses++);
}
Cameron Grande
  • 163
  • 1
  • 10