How do i do a method twice, but with different arguments?
I have two, 2D arrays. I have a method that i call twice, the first time to get user input for the first array, and the second time to get user input for the second array. How do i do this tho, becuase i will only have 1 array in the method at a time being assigned values. so how do i have 2 arrays with values?
this is the part im working on.
Code:
import java.util.Scanner;
public class Lab3
{
public static void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
int menuChoice = 0;
int rows = 0, cols = 0;
System.out.println("How many rows are there?");
rows = keyboard.nextInt();
System.out.println("How many columns are there?");
cols = keyboard.nextInt();
int[][] arrayOne = new int[rows][cols];
int[][] arrayTwo = new int[rows][cols];
int[][] arrayThree = new int[rows][cols];
System.out.println("Choose from the menu:" + "\nAdd two arrays: 1" +"\nSubtract two arrays: 2" + "\nExit: 3");
menuChoice = keyboard.nextInt();
do
{
switch(menuChoice)
{
case 1:
{
System.out.println("Enter data for first array.");
arrayOne = arrayInput(arrayOne);
System.out.println("Enter data for second array.");
arrayTwo = arrayInput(arrayTwo);
arrayAdd(arrayOne, arrayTwo);
break;
}
case 2:
{
System.out.println("Enter data for first array.");
arrayOne = arrayInput(arrayOne);
System.out.println("Enter data for second array.");
arrayTwo = arrayInput(arrayTwo);
arraySub(arrayOne, arrayTwo);
break;
}
case 3:
System.exit(0);
break;
}
}while(menuChoice != 3);
}
public static int[][] arrayInput(int[][] array)
{
Scanner keyboard = new Scanner(System.in);
for(int row = 0; row < rows; row++)
{
for(int col = 0; col < cols; col++)
{
array[row][col] = keyboard.nextInt();
}
}
return array;
}
public static void arrayAdd(int[][] arrayOne, int[][] arrayTwo)
{
int[][] arrayThree;
for(int i = 0; i < arrayOne.length; i++)
{
for(int j = 0; j < arrayOne[i].length; j++)
{
arrayThree[i][j] += arrayTwo[i][j];
}
}
}
public static void arraySub(int[][] arrayOne, int[][] arrayTwo)
{
}
}
also, i cant figure out how to pass the sizes of the arrays into methods. it says it cant find rows,cols, that are declared.
I:\Documents\Spring 2010\Computer Science II\Lab3.java:62: cannot find symbol
symbol : variable rows
location: class Lab3
for(int row = 0; row < rows; row++)
^
I:\Documents\Spring 2010\Computer Science II\Lab3.java:64: cannot find symbol
symbol : variable cols
location: class Lab3
for(int col = 0; col < cols; col++)
ALSO, do you see any other problems in my code, or problems i will run into?