John Lapinski
John Lapinski

Reputation: 53

Problems printing a multidimensional array

I created a program that asks the user for a row length and a column length and then creates a "multidimensional array". It then fills it with zeros. Later it will fill each component with a random number and then calculate the sum of all the adjacent cells and add the number to its current position in a new array of the same size. Right now I'm just printing out the array with zeros but when I do it prints out way more sets of arrays than I need. It seems to be printing out "row" times to many but I don't see why. Any help you guys can throw my way?

import java.util.Scanner;

public class LAB1 {

public static void main(String args[]){

    Scanner keyboard = new Scanner(System.in);

    int row, column;


    //Ask user for row size
            System.out.print("Enter an integer from 5 to 20 for row size: ");
    //Make sure input is an int
            while(!keyboard.hasNextInt()){
                System.out.println("Not an integer. Try again: ");
                keyboard.next();
                }
    //Checks if input is out of bounds  
            row = keyboard.nextInt();
            while(row<5 || row>20){
                System.out.println("Not a valid row size. Please try again: ");
                row=keyboard.nextInt();}
    //Tells user row size
            System.out.println("Row size: "+row);       
    //Ask user for column size
            System.out.print("Enter an integer from 5 to 20 for row size.\nIt must be different from row size: ");
            column = keyboard.nextInt();
    //Checks if input is out of bounds or repeated 
            while(column==row || column<5 || column>20){
                    System.out.println("Not a valid column size. Please try again: ");
                    column=keyboard.nextInt();}
    //Tells user column size
            System.out.println("Column size: "+column);

            ProcessArray.ProcessArray(row, column);

                    }   
}


import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class ProcessArray{

private static int firstArray[][];
//private int secondArray[][];

static int[][] ProcessArray(int row, int column){
    firstArray = new int[row][column];
    ProcessArray.initializeArray(firstArray);
    return firstArray;
}

//fill first array with zeros
    public static void initializeArray(int[][] firstArray){
        for(int[] row: firstArray){
            Arrays.fill(row, 0);
            System.out.print(Arrays.deepToString(firstArray));

        }
    }
}

Upvotes: 1

Views: 62

Answers (1)

Scary Wombat
Scary Wombat

Reputation: 44824

You are printing out the entire Array for every element of the Array

See

for(int[] row: firstArray){
        Arrays.fill(row, 0);
        System.out.print(Arrays.toString(firstArray));

}

try

for(int[] row: firstArray){
        Arrays.fill(row, 0);
        System.out.print(Arrays.deepToString(row));

}

or

for(int[] row: firstArray){
        Arrays.fill(row, 0);
 }
 System.out.print(Arrays.deepToString(firstArray));

Upvotes: 1

Related Questions