Good day, colleagues. There is one code that outputs a table of random boolean values ​​to the console. Everything would be fine, but I need the values ​​not to be output by the stream, one after the other, but changed in real time. Those by analogy with the clock in the console: the time should not be written in a new line with each new second, but should be, and updated in the original.

boolean[][] dots = new boolean[5][5]; Random randomBoolean = new Random(); //Now i want to create a table with random boolean values //Eternal loop for (;;) { try { Thread.sleep(500); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { dots[i][j] = randomBoolean.nextBoolean(); System.out.print(dots[i][j] + "\t"); } //Close the column System.out.println(); } System.out.println(); } catch (InterruptedException e) { } } 

What and where do I need to add for this? thank

  • This is java .. Vryal di here will manage to add something easy for this ... - Qwertiy ♦
  • Never heard of such a thing. Here is what stackoverflow English says: stackoverflow.com/questions/2979383/java-the-console - angry
  • I tried to use the tips from the English stack, but it did not work = ( - VOV

1 answer 1

Java itself does not know how to clear the console. But there is a small library that allows sending control sequences to any ANSI-compatible console β€” Jansi :

 import java.util.Random; import org.fusesource.jansi.AnsiConsole; public class Main { public static void main(String[] args) { AnsiConsole.systemInstall(); Random randomBoolean = new Random(); for (;;) { System.out.print("\u001b[2J"); System.out.flush(); try { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { System.out.print(randomBoolean.nextBoolean() + "\t"); } System.out.println(); } System.out.println(); Thread.sleep(500); } catch (InterruptedException e) {} } } } 
  • Actually it works without a library, maybe it should be so? :) - Alex Chermenin
  • @AlexChermenin on Windows too? - Sergey Gornostaev
  • And it is better to use the sequence \u001b[6F , and after the output (go back up 6 lines and print again). More information about the sequence is in the wiki: ru.wikipedia.org/wiki/… - Alex Chermenin
  • so far there is no way to check how it will work out - accomplish your goal - Alex Chermenin