Results 1 to 7 of 7
  1. #1
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default 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.

    Java 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The rows and columns can be obtained from the arrays themselves via the length property.

    Java Code:
    int rows = myArray.length;
    int cols = myArray[0].length; // if the array is not a "ragged" array and all columns are same size

  3. #3
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    does that declaration go in my method? why does the col get a [0]. and what about my main question. i call the method twice, and pass one array in each of them. but in the method how do i differentiate between the arrays, so they dont replace each others values?
    Last edited by Meta; 04-05-2010 at 08:44 PM.

  4. #4
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    ok. i figured most of this out. the only thing im still working on is outputting the results in proper matrix format.

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    It doesn't do you much good to have your arrayAdd() and arraySub() (call it arraySubtract(), by the way) declared as void -- you're storing your results in arrayThree, and then throwing it away!

    To answer your specific questions, I would define arrayInput() this way:
    Java Code:
            public static int[][] arrayInput(int rows, int cols) {
                    int[][] result = new int[rows][cols];
                    ...
                    return result;
            }
    Then there is no need to initialize your arrays in the main() method -- just declare them as you call the method, like this:
    Java Code:
            int[][] arrayOne = arrayInput(rows, cols);
    Then within your arrayAdd() and arraySubtract() methods you get the size of the arrays as Fubarable said. Keep in mind that a Java "two-dimensional" array is really an array of arrays. So myArray.length tells you how many arrays of ints you have, and myArray[0].length tells you how many elements are in that first array -- and we assume that the rest of the arrays are of the same length. In this case it's a safe assumption, as we know how arrayInput is declaring them. In other cases it might not be a safe assumption, and you should query myArray[i].length for each "row".

    I don't understand your question about "replace each others values" -- if you pass arrayOne into a method as a parameter, then nothing will happen to arrayTwo -- the method won't even have access to it. Where you are passing two arrays to a method, it's up to you whether you want to modify them or not. Your arrayAdd() and arraySubtract() methods don't modify the parameter arrays -- they both update a new arrayThree (which, by the way, you will need to initialize within the method -- and don't bother initializing an arrayThree in main() because you are not using it).

    Overall, it seems you are having a little trouble getting the concept of variable scope. Maybe it has not been explained to you. Variables in Java are limited to the scope in which they are declared. Scope can be an execution block (such as a for loop or a while loop), a method, an object, or a class. A method can see any variables declared within it, variables that are passed as parameters, and instance variables of the method's class. It cannot see any variables declared elsewhere. So arrayAdd() cannot see the rows and cols variables you declare in main(). If you want arrayAdd to see those values, you need to pass them as parameters (but in this case it's not necessary because arrayAdd can get the size of the array by looking at the .length property).

    I know it's confusing, and I hope I haven't made it worse. Read through your text again and walk through your code mentally, or with pencil and paper. You'll get it soon enough.

    -Gary-

  6. #6
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    ok guys. thanks for the help. i made some changes to my code. and it does what i want it to do. but theres jsut one thing i dont like, but i feel like it will destroy my code if i try to fix it. basically what is happening is im asking for the rows and columns before the menu, so they have to type that even if they plan on exiting the program, which is not user friendly. but if i move that stuff into the input method, i lose so many symbols and variables and get so many errors. heres my new code.

    also, i dont know if i should make methods to display the output or not..

    Java 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];
    		
    		do
    		{
    			System.out.println("Choose from the menu:" + "\nAdd two arrays: 1" +"\nSubtract two arrays: 2" + "\nExit: 3");
    			menuChoice = keyboard.nextInt();
    	
    			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 < array.length; row++)
    		{
    			for(int col = 0; col < array.length; col++)
    			{
    				array[row][col] = keyboard.nextInt();
    			}
    		}
    		return array;
    	}
    	
    	public  static void arrayAdd(int[][] arrayOne, int[][] arrayTwo)
    	{
    		for(int i = 0; i < arrayOne.length; i++)
    		{
    			for(int j = 0; j < arrayOne[i].length; j++)
    			{
    				System.out.print(arrayOne[i][j] + arrayTwo[i][j]+ " ");
    				
    			}
    			System.out.println();
    		}		
    	}
    	
    	public static void arraySub(int[][] arrayOne, int[][] arrayTwo)
    	{
    		for(int i = 0; i < arrayOne.length; i++)
    		{
    			for(int j = 0; j < arrayOne[i].length; j++)
    			{
    				System.out.print(arrayOne[i][j] - arrayTwo[i][j]+ " ");
    				
    			}
    			System.out.println();
    		}
    	}
    }

  7. #7
    Meta is offline Member
    Join Date
    Mar 2010
    Posts
    78
    Rep Power
    0

    Default

    omg why doesnt this work. i made an if statement after the do so i only ask for input if they are not exiting. but it doesnt work. now it saays it cant find the variables. why is it even trying to do the switch statement BEFORE the IF. WTH.

    Java 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;
    		
    		do
    		{
    			System.out.println("Choose from the menu:" + "\nAdd two arrays: 1" +"\nSubtract two arrays: 2" + "\nExit: 3");
    			menuChoice = keyboard.nextInt();
    			
    			if(menuChoice != 3)
    			{
    				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];
    			}
    				
    			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);
    	}

Similar Threads

  1. [SOLVED] method not applicable for the arguments
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-31-2009, 04:13 PM
  2. method that return 2 arguments
    By itaipee in forum New To Java
    Replies: 19
    Last Post: 01-12-2009, 05:36 PM
  3. Using final with method arguments
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:48 PM
  4. Arguments in Main
    By CyberFrog in forum New To Java
    Replies: 2
    Last Post: 03-30-2008, 09:37 PM
  5. Variable No. of Arguments
    By Gajesh Tripathi in forum New To Java
    Replies: 2
    Last Post: 10-31-2007, 02:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •