Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java help PLEASE! I currently have this: public class multi_Array { public stati

ID: 3661573 • Letter: J

Question

Java help PLEASE!

I currently have this:

public class multi_Array {

   public static void main(String args[]) {

       int data[][][];
       data = createData();
       printData(data);
  
   }

   public static void printData(int[][][] array) {

        for (int i = 0; i < array.length; i++) {
            System.out.println("Frame " + i + ":");
            for (int j = 0; j < array[i].length; j++) {
                for (int k = 0; k < array[i][j].length; k++) {
                    System.out.print(" " + array[i][j][k] + " ");
                }
                System.out.print(" ");
            }
        }

   }

   public static int[][][] createData() {

        int[][][] data;

        Random rand = new Random();
        int x = rand.nextInt(5) + 1;
      
        //rand = new Random();
        int y = rand.nextInt(5) + 1;
      
        //rand = new Random();
        int z = rand.nextInt(5) + 1;

        data = new int[x][y][z];
        System.out.println(x + " " + y + " " + z);

        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                for (int k = 0; k < z; k++) {

                    rand = new Random();
                    int r = rand.nextInt(5) + 1;
                    data[i][j][k] = r;
                }
            }
        }

        return data;
   }

}

it compiles fine but is not implementing exactly what i want,when i run it i get something like this: (random result everytime)

Frame 0:
   3    5    5    4
Frame 1:
   1    2    5    4
Frame 2:
   4    4    5    2
Frame 3:
   4    3    3    1

My problem is each frame should have a random amount of rows and columns but mine are all the same in length.. PLEASE ANY HELP WOULD BE GREAT!

Explanation / Answer

Your code is absolutly correct. but there is a problem with domain values of internal array.

try with some like:

int x= rand.nextInt(50);

int y= rand.nextInt(50);

int z= rand.nextInt(50);

int r= rand.nextInt(50);

then the result will be different.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote