0

I got the 2d array to print but with all zero's and the only random number comes up on the bottom right corner

How do I get the code to print random numbers in all the elements of the 2d array?

Here is my code:

public static void main(String[] args) {
    int columns = 8;
    int rows = 4;
    int rLow = 2;
    int rHigh = 9;
    printRandos(columns, rows, rLow, rHigh);
}

public static void printRandos(int clmn, int rws, int rlow, int rhigh) {
    Random rando = new Random();
    int randoNum = rlow + rando.nextInt(rhigh);
    int[][] randoArray = new int[rws][clmn];
    for (int i = 0; i < rws; i++) {
        for (int k = 0; k < clmn; k++) {
            randoArray[rws - 1][clmn - 1] = randoNum;
            System.out.print(randoArray[i][k] + " ");
        }
        System.out.print("\n");
    }
}
Junior
  • 79
  • 8
  • Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Ivar May 22 '21 at 10:25
  • Array indexes are zero-based. So when you create an array with 3 elements, the indexes are 0, 1 and 2. When you try to access 3, it will throw an exception. In other words, the last element of your array is `randoArray[rws - 1][clmn - 1]`. Though in your example your array doesn't seem to do anything at all. You might as well just remove it. – Ivar May 22 '21 at 10:26
  • You are using the constant values `rws-1` and `clmn-1` instead of the loop counters `i` and `k`. – knittl May 22 '21 at 11:34

4 Answers4

2
for (int i = 0; i < rws; i++)
{
    for (int k = 0; k < clmn; k++)
    {

       int randoNum = rlow + rando.nextInt(rhigh);
       randoArray[i][k] = randoNum;
               
       System.out.print(randoArray[i][k]+" ");
    }
    System.out.print("\n");
}
Riz
  • 368
  • 4
  • 18
1

Your bug is in this line:

          randoArray[rws-1][clmn-1] = randoNum;

This stores your random number into randoArray[rws-1][clmn-1] each time, which as you noticed, is the bottom right corner. rws is always 4, and clmn is always 8. So you store the same number there 32 times, which gives the same result as storing it only once.

In the following line you are correctly printing the number from the current array location:

          System.out.print(randoArray[i][k]+" ");

An int array comes initialized with all zeroes, and since except for the last corner you have not filled anything into your array, 0 is printed.

Also if you want different random numbers in all the cells, you would need to call rando.nextInt() inside your innermost for loop.

Unless you need this 2-D array for some purpose (which doesn't show from the minimal example code that you have posted), you do not need it for printing a matrix of random numbers, i.e., you may just print the numbers form within your loop without putting them into the array first.

Finally if rhigh should be the highest possible random number in the array, you should use rando.nextInt(rhigh - rlow + 1). With rlow equal to 2 and rhigh equal to 9 this will give numbers in the range from 0 inclusive to 9 - 2 + 1 = 8 exclusive, which means that after adding to rlow = 2 you will get a number in the range from 2 to 10 exclusive, in other words, to 9 inclusive.

I am on purpose leaving to yourself to fix your code based on my comments. I believe your learning will benefit more from working it out yourself.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

your mistake inside the inner for loop of the printRandos method. Firstly your random number is outside the loop so your array elements were receiving the same number all the time. Another mistake is that you are assigning the value to the same array element all the time i.e rws-1 and clmn-1 . inside your inner loop replace it with this:

           int randoNum = rlow + rando.nextInt(rhigh);
           randoArray[i][k] = randoNum;
           System.out.print(randoArray[i][k]+" ");
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
-1

Your assign the array value outside the array length

 int[][] randoArray = new int[rws][clmn];
 randoArray[rws][clmn] = randoNum;

Here randoArray[rws] is out of bounds.

Stefan Fenn
  • 483
  • 5
  • 13