Printing 10 integers per line

Viewed 98

I have this code. This code simply creates an array of size 1,000,000 to hold randomly generated integers. My problem is that i want to print 10 integers per line and the output is so strange and i don`t know why. I debugged the code and found that the code must work as expected. Though, it acts strangely. I will be grateful if anyone can help me. This is my code:

Integer[] list = new Integer[1000000];
for(int i = 0 ; i < list.length ; i++)
list[i] = (int)(Math.random() * 1000);
 
//print 10 integers per line
for(int i = 0 ; i < list.length ; i++){
if( (i+1) % 10 == 0)
  System.out.printf("%-5d\n" , list[i]);
else
  System.out.printf("%-5d" , list[i]);
}

I attach a screenshot of the output. enter image description here

By the way: I use Intellij IDEA.

1 Answers

Console cycle buffer size is 1024 KB (default for Intellij idea), you can see it in Settings -> Editor -> General -> Console. So your 1,000,000 random numbers more than that. In the first line of your console after program is executed, you do not see real first line, it is some where in the middle of process. So, that line is cut. If you print 1000 numbers, you will see that your code works fine.


Edit:

In above-mentioned setting's location, there is an option Override console buffer size, click on the check box, increase buffer size. When size is big enough, you can see all your numbers in the form of 10 numbers per line.

Related