Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3752740
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:17:18+00:00 2026-05-19T09:17:18+00:00

I have an assignment that I have to create a randomly sized 3D array,

  • 0

I have an assignment that I have to create a randomly sized 3D array, write it into a a binary file, then read the binary file back into the program and create another 3D array that’s the same as the first. I’m having problems reading back into the program, after hours I can only get the first int, or the last, from the prior array. I haven’t gotten into passing the first 2D’s yet so I just allocated some space to make the array work, but once I get this that should come quickly. The readData() method is the one giving me problems. Thanks in advance.

import java.io.*;
import java.util.*;

public class homework1 {

public homework1() {
}

// Allocates space for the 3-dimension array as specified and for each
// array element, assigns a random number, and return the array
public static int[][][] createData() {

    int[][][] data;

    //Random variables for array dimensions
    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];

    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;
}

//Writes the 3-dimension array to file.
public static int[][][] writeData(int[][][] array, String fileName)
        throws IOException {

    try {
        FileOutputStream out = new FileOutputStream(fileName);
        DataOutputStream outs = new DataOutputStream(out);

        for (int i = 0; i < array.length; i++) {
            //outs.writeInt(array[i].length); (maybe?)

            for (int j = 0; j < array[i].length; j++) {
                //outs.writeInt(array[i][j].length); (maybe?)

                for (int k = 0; k < array[i][j].length; k++) {
                    outs.writeInt(array[i][j][k]);
                }
            }
        }

        outs.close();
        out.close();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return array;
}

public static int[][][] readData(String fileName)
        throws FileNotFoundException, IOException {

    int[][][] array = new int[3][3][5];

    try {
        FileInputStream in = new FileInputStream(fileName);
        DataInputStream ins = new DataInputStream(in);
        int readFrom = ins.readInt(); //read 4 binary byes and

        System.out.println("From file");




        while (in.read() != -1) {
           // poop = ins.readInt();
            System.out.println(readFrom);

            for (int i = 0; i < array.length; i++) {
                //outs.writeInt(array[i].length); (maybe?)

                for (int j = 0; j < array[i].length; j++) {
                    //outs.writeInt(array[i][j].length); (maybe?)

                    for (int k = 0; k < array[i][j].length; k++) {
                        array[i][j][k] = readFrom;
                    }
                }
            }

            System.out.flush();

            readFrom=ins.readInt();

        }
        //save them in an integer
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (EOFException ex){
        ex.printStackTrace();
    }
    System.out.println("Blank array that needs to be filled");

    return array;
}

// Displays the array.
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("\t" + array[i][j][k] + " ");
            }
            System.out.print("\n");
        }
    }

}

public static void main(String args[]) throws IOException {
    //        throws FileNotFoundException, IOException {

    int data[][][];
    data = createData();
    printData(data);
    writeData(data, "data.out");
    data = readData("data.out");
    printData(data);

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:17:18+00:00Added an answer on May 19, 2026 at 9:17 am

    As you have written it, you don’t read from the file each time you loop in the innermost loop — and not either in the outer loops. So you read only once.

    The ins.readInt() call should be in the innermost loop, because you need to read each table cell.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an assignment said that to create a findString function that accept 2
hey guys, i have a c++ programming assignment that asks me to create a
As part of an assignment I need to create a shipping program that checks
I currently have a school assignment that involves both PHP and asp.net. Now the
I'm working on an assignment that is telling me to assume that I have
For a school assignment I have to write x86 assembly code, except I can't
For an assignment I have to write a Tribool class in C# using a
I have an assignment to create a GUI using MATLAB GUIDE and am having
I have a database assignment which I have to create some relational algebra for
I have a class X that I would like to put into an STL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.