This program creates an integer array with a random size of 5-50 elements (inclusive). It then fills the array with random numbers between 0 - 100 (inclusive). The interesting part comes when I then have to print the array as a table limited at 5 columns wide. Also, the array must be centered on an output screen 80 characters wide.
My main concern and question is: how do I efficiently move through an array of an unknown size?
My solution is basically to count through each element, keeping track of how many are left and printing new whole rows while there are still any elements left, and checking each element to see if it should be empty or not.
But would division be possible? My main issue with division is if I were to do rows = NUMBERS / 5 + 1
, I would always end up with 1 extra empty row whenever NUMBERS
was divisible by 5.
import java.util.Random;
public class NumberGenerator
{
private static final int MIN_INTS = 5, MAX_INTS = 50; //Range of numbers to be generated
private static final int MIN_RANGE = 0, MAX_RANGE = 100; //Legal range of generated numbers
private static final int TABLE_COLUMNS = 5; //Number of columns in output table
private static Random generator = new Random(); //Random number generator
/**********************************************************
* BEGIN Main
* Calc number of integers to generate between 5 and 50 (inclusive)
* Init array the size of the generated number
* Init current number to 0
* FOR (each number in array) DO
* Assign random number between 0 and 100 to current cell
* END FOR
* Display title
* Display top of table
* WHILE (there are numbers not displayed) DO
* Display left edge of table
* FOR (each cell in table row) DO
* IF (numbers are left in the array)
* Display formatted cell value and cell's right edge
* ELSE
* Display empty cell and cell's right edge
* END IF
* Increment current number
* END FOR
* Display EOL
* END WHILE
* Display bottom of table
* END Main
**********************************************************/
public static void main (String [] args)
{
final int NUMBERS; //Number of integers to generate
final String BORDER = "+-----------------------------+"; //Top and bottom border
final String PAD_FORMAT_STR; //Printf format for left pad
int[] numberList; //Array of generated integers
int tablePadding = 24; //Number of columns in front of output table
int currentNumber; //Current number drawn in table
NUMBERS = generator.nextInt(MAX_INTS - MIN_INTS + 1) + MIN_INTS;
PAD_FORMAT_STR = "%" + tablePadding + "s%s";
numberList = new int[NUMBERS];
for (int i = 0; i < NUMBERS; i++)
{
numberList[i] = generator.nextInt(MAX_RANGE + 1); //+1 to include max bound
}
currentNumber = 0;
System.out.printf("%32s%s", "", "Number Generator\n\n");
System.out.printf(PAD_FORMAT_STR + "\n", "", BORDER);
while (currentNumber < NUMBERS)
{
System.out.printf(PAD_FORMAT_STR, "", "|");
for (int i = 0; i < TABLE_COLUMNS; i++)
{
if (NUMBERS > currentNumber)
{
System.out.printf(" %3d |", numberList[currentNumber]);
}
else
{
System.out.print(" |");
}
currentNumber++;
}
System.out.println();
}
System.out.printf(PAD_FORMAT_STR + "\n", "", BORDER);
}
}
An example of output is:
Number Generator +-----------------------------+ | 40 | 3 | 79 | 2 | 37 | | 80 | 2 | 82 | 26 | 89 | | 1 | 88 | 21 | 5 | 45 | | 100 | 5 | 100 | 78 | 94 | | 56 | 31 | | | | +-----------------------------+
rows = (NUMBERS + 4) / 5;
is the expression you are looking for. Another way would berows = (NUMBERS / 5) * 5;
. \$\endgroup\$i * 5 + j - 1
withi
counting the rows andj
counting the table columns and both starting at 1 instead of 0? \$\endgroup\$