# Returning 2D array from method

**843789**Apr 19, 2009 10:00 PM

Hi all. I'm working on a lab (simple stuff by all standards, but I'm still quite "green"). We're being introduced to returning 2D arrays/ passing them. I've looked at several similar examples and scoured a book, and for the life of me don't know why this won't compile. The errors are below it. I'm sure my answer is within the error message but I just don't see it. Thanks in advance...

```
//Lab 8
//Input: Size of arrays, integers for arrays, choice of addition, subtraction, or multiplication.
//Output: Matrix result of arithmetic on two arrays.
//Get the size of two arrays and fill them with integers. Let the user choose addition, subtraction, or
//multiplication of those arrays and display it in a matrix format.
import java.text.*; //for DecimalFormat class
import java.util.*; //for Scanner class
public class Lab82DArraysKevinRice2
{
public static void main(String[] args)
{
//Variable declarations
int arrayLength = 0;
int arithmeticChoice = 0;
Scanner keyboard= new Scanner(System.in);
//Begin main method with an introduction and request the size of the arrays.
//Enter -1 to quit.
System.out.println("Welcome to array arithmetic.");
System.out.println("Enter the number of rows and columns for the arrays.");
System.out.println("Just one number since #rows = #columns. Enter -1 to exit.");
arrayLength = keyboard.nextInt();
if(arrayLength > 0)
{
//Initialize each of 3 arrays and call the method to load them.
int [] [] firstArray = new int [arrayLength] [arrayLength];
loadArray (firstArray);
int [] [] secondArray = new int [arrayLength] [arrayLength];
loadArray (secondArray);
int [] [] totalsArray = new int [arrayLength] [arrayLength];
System.out.println("Would you like to add, subtract, or multiply the arrays?");
System.out.println("Enter 1 to add, or 2 to subtract (array1 from array2).");
arithmeticChoice = keyboard.nextInt();
switch(arithmeticChoice)
{
case 1:
totalsArray = addArrays(firstArray, secondArray);
outputArray(firstArray);
System.out.println("+");
outputArray(secondArray);
System.out.println("=");
outputArray(totalsArray);
break;
case 2:
subtractArrays(firstArray, secondArray);
break;
}
}
System.out.println("Array arithmetic will end now.");
}
public static void loadArray (int [] [] arrayName)
//Loads an array of integers. Array is passed.
{
Scanner keyboard= new Scanner(System.in);
System.out.println("Enter " + (arrayName.length * arrayName.length) + " integers.");
//Outer, or row loop.
for(int i = 0; i < arrayName.length; i ++)
{
//Inner loop to fill columns across each row.
for(int j = 0; j < arrayName.length; j ++)
{
arrayName
```* [j]= keyboard.nextInt();*

}

}

}

public static int addArrays (int [][] array1, int [][] array2)

{

int [] [] equalsArray = new int [array1.length] [array1.length];

for(int i = 0; i < array1.length; i ++)

{

for(int j = 0; j < array1.length; j ++)

{

equalsArray [i] [j]= array2 [i] [j] - array1 [i] [j];

}

}

return equalsArray;

}

public static int subtractArrays (int [][] array1, int [][] array2)

{

int [] [] equalsArray = new int [array1.length] [array1.length];

for(int i = 0; i < array1.length; i ++)

{

//Inner loop to fill columns across each row.

for(int j = 0; j < array1.length; j ++)

{

equalsArray [i] [j]= array1 [i] [j] - array2 [i] [j];

}

}

}

public static void outputArray (int [][] a)

{

for(int i = 0; i < a.length; i ++)

{

//Inner loop to fill columns across each row.

for(int j = 0; j < a.length; j ++)

{

System.out.println(a [i] [j]);

System.out.print(" ");

}

}

}

}
`The errors:`

----jGRASP exec: javac -g F:\COS102\8\Lab82DArraysKevinRice2.java

Lab82DArraysKevinRice2.java:47: incompatible types

found : int

required: int[][]

totalsArray = addArrays(firstArray, secondArray);

^

Lab82DArraysKevinRice2.java:91: incompatible types

found : int[][]

required: int

return equalsArray;

^

2 errors

----jGRASP wedge2: exit code for process is 1.

----jGRASP: operation complete.
` `

- 724 Views
- Tags: none (add)