Cleared or overwritten Java printed lines stopping program

3 hours ago 1
ARTICLE AD BOX

I am working on a program in Java that mimics a screen by reading and printing a 2D array. The program prints out the array, waits for a specified duration, and then runs the lines System.out.flush(); System.out.print("\033[H"); in an attempt to clear the output. It appears to do this, but slows down and stops after enough time has passed despite the fact that only the last frame is shown in the box if you scroll up or down. If I use a different IDE, it kills the process earlier because the output exceeds 2 MB.

The whole reason that I used text instead of a screen is because I cannot import many modules. My best guess, I am fairly new to non-scratch programming, is that I cannot use anything that requires you to access the terminal to import, or are not immediately available on a free online IDE, but I don't know what exactly I can and can't import.

System.out.print("\033\143");

has the same issue.

System.out.println(new String(new char[50]).replace("\0", "\r\n"));

does not properly clear the output.

Is there a way to clear the output so that the code can run for a long time without reducing performance significantly?

Here is a sample of code that recreates this effect:

public class Tester { public static void main(String[] args) { int rows = 50; int cols = 50; while(true) { for(int i = 0; i<rows; i++) { for(int g = 0; g<cols; g++) { System.out.print("a"); } System.out.print("\n"); } //thing that clears output System.out.flush(); System.out.print("\033[H"); } } }
Read Entire Article