Question

JAVA CODE Write a program which has a 3D array and prints the integers from the...

JAVA CODE

Write a program which has a 3D array and prints the integers from the array. Then then convert the output into a file using filewriter

Homework Answers

Answer #1
import java.io.FileWriter;
import java.io.IOException;

public class Array3DToFile {
    public static void main(String[] args) {
        int arr[][][] = {
                {
                        {1,2,3,4},
                        {5,6,7,8}
                },
                {
                        {8,7,6,5},
                        {4,3,2,1}
                }
        };

        for(int i = 0;i<arr.length;i++){
            for(int j = 0;j<arr[i].length;j++){
                for(int k = 0;k<arr[i][j].length;k++){
                    System.out.print(arr[i][j][k]+"\t");
                }
                System.out.println();
            }
            System.out.println();
        }


        FileWriter fw= null;
        try {
            fw = new FileWriter("output.txt");
            for(int i = 0;i<arr.length;i++){
                for(int j = 0;j<arr[i].length;j++){
                    for(int k = 0;k<arr[i][j].length;k++){
                        fw.write(""+arr[i][j][k]+"\t");
                    }
                    fw.write("\n");
                }
                fw.write("\n");
            }
            fw.close();

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

    }
}

1   2  3  4  
5  6  7  8  

8  7  6  5  
4  3  2  1  

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write Java code that creates an array of primitive integers named odds and stores all odd...
Write Java code that creates an array of primitive integers named odds and stores all odd numbers between -6 and 38 using a for loop. (Your code will populate the array.) Your code should also enable the array’s size to be exactly large enough to store the numbers. (You will not put a number for the size, your code will determine the number.) (5 pt)
JAVA Write a program that has two functions in which the user chooses which one to...
JAVA Write a program that has two functions in which the user chooses which one to perform; 1. reads in a CSV file of integers into an array and insert it into a binary search tree class 2. deserializes an object and inserts it into a binary search tree class the tree class must be in a separate class from the CSV file read and deserialize object load
iNTRODUCTION TO JAVA PROGRAMING Write a program that prints a triangle using star (*) maximum star...
iNTRODUCTION TO JAVA PROGRAMING Write a program that prints a triangle using star (*) maximum star (10) Program output ********** ********* ******** ******* ****** ***** **** *** ** *
Write an application in Java which includes an algorithm that takes an array of any size,...
Write an application in Java which includes an algorithm that takes an array of any size, selects the high and low integer from the array of integers with each pass and builds a new array of integers by inserting the high and low selection with each pass. Your output should show the original array of integers and the output of each pass on a new line. Note: You can assume all integers are positive, but your code must work for...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds)
C program- using visual studio code. Write a program that asks for an integer and then...
C program- using visual studio code. Write a program that asks for an integer and then prints all the alternative integers from (and including) that value up to (and including) a value larger by 20. (That is, if the input is 5, the output runs from 5 to 25.) Be sure to separate each output value by a space or tab
Using Java code and ARRAYS, please write a program that takes in the names of four...
Using Java code and ARRAYS, please write a program that takes in the names of four individuals and prints out all the names. The number of names (4) is predetermined.
Write a program that uses an array of integers initialized to whatever values you wish. Write...
Write a program that uses an array of integers initialized to whatever values you wish. Write a methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same values as the original plus 10. (num +=10) In Java
Write a function that takes an array of integers and its size as parameters and prints...
Write a function that takes an array of integers and its size as parameters and prints the two largest values in the array. In this question, the largest value and the second largest value cannot be the same, even if the largest value occurs multiple times. In the first sample, the two largest values are 9 and 8 (even though the value 9 appears twice). In the second sample, all the values are equal and the program recognizes this by...