-2

I'm using this code:

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

public class Matrix
{
    public static void main(String[] args)
    {
        Scanner enter = new Scanner(System.in);
        int[][] firstMatrix = new int[4][4];
        int[][] secondMatrix = new int[4][4];
        Random spin = new Random();



        System.out.println("Please enter 16 numbers to populate the array: ");

        for (int count = 0; count<firstMatrix.length;count++) // nested for for user input to populate 4x4 array
        {


            for (int count2 = 0; count2 < firstMatrix[count].length; count2++)
            {

                firstMatrix[count][count2] = enter.nextInt(); // populate array with user input



            }

        } // end array population

            System.out.printf("The sum is: %d%n", sumMatrix(firstMatrix)); // call sumMatrix
            System.out.println(firstMatrix[0][0]); // debug 


            for (int count3 = 0; count3<secondMatrix.length; count3++) // nested for to populate array with random numbers 1-100, row
            {
                for (int count4 = 0; count4<secondMatrix[count3].length; count4++) // column 
                {
                    secondMatrix[count3][count4] = 1 + spin.nextInt(100); // 100 inclusive, generate and populate array
                }
            }

             System.out.println(secondMatrix[0][0]); // debug to show that it is properly printing the correct element

             for (int i = 0; i<secondMatrix.length; i++)
             {
                for (int j = 0; j<secondMatrix[i].length; j++)
                    System.out.print(" " + secondMatrix[i][j]); // print the total array ( this process can be used to print the returned array )


             }

        System.out.println(); // debug

             int arrayTotal = firstMatrix[0][0] + secondMatrix[0][0]; // debug
                System.out.println("The element total is " + arrayTotal); // debug
                    System.out.println();

        addMatrix(firstMatrix,secondMatrix); // call addMatrix






        System.out.println("Programmed by Stephen Mills");
    }  // end main      

        public static int sumMatrix(int[][] array) // method to sum the elements of the array
        {
                int total = 0;

                for (int number = 0; number<array.length; number++) // row 
                {
                    for (int number2 = 0; number2<array[number].length; number2++) // column
                        total += array[number][number2]; // sum of all elements
                }
                    return total; // returns the sum

        } // end sumMatrix

        public static int[][] addMatrix(int[][] array1, int[][] array2) // improper method
        {
            int[][] thirdMatrix = new int[4][4];

            for (int count4 = 0; count4<thirdMatrix.length; count4++)
            {
                for (int count5 = 0; count5<thirdMatrix[count4].length; count5++)
                    thirdMatrix[count4][count5] = array1[count4][count5]+array2[count4][count5];


            }
            System.out.println(Arrays.toString(thirdMatrix));
            return thirdMatrix;
        } // end addMatrix7



}

Most of the program works as intended but I'm getting this "[[I@3d4eac69, [I@42a57993, [I@75b84c92, [I@6bc7c054" output when trying to print my array. I've tried several methods, including simple println, toString, setting a variable equal to the method call, and I've tried putting the print statement in main first and then in the method second. Any ideas why this is happening?

Eric F.
  • 86
  • 5
srmjr
  • 97
  • 1
  • 1
  • 9

1 Answers1

0

If you want to print int elements of the 2d array thirdMatrix you can try this:

for(int[] simpleArray: thirdMatrix) 
        System.out.println(Arrays.toString(simpleArray));

The reason of your output: when you call

Arrays.toString(thirdMatrix)

you will call

String.valueOf(element)

foreach element of thirdMatrix. As elements of thirdMatrix are Array not primitive types, method

String.valueOf(Object obj) 

will be called which return the hashcode of thirdMatrix elements(one-dimensional arrays).

  • Wow. This solved my problem. I can't tell you how grateful I am. I've been working on this project all day. How do I set this as answered? Sorry, first post. edit: well, one more thing, can you use printf using this method, and if so, what format specifier would you use? Thank you! – srmjr Jul 24 '16 at 05:07
  • You can use printf in a nested for loop like this: for(int[] OneDArray : thirdMatrix) for(int element : OneDArray) System.out.printf("%d",element); "%d" is used for int values. – Ehsan Jahangiri Jul 24 '16 at 05:22