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

    Default 2d Arrays Code help!

    Im trying to make code where there are 2, 2D arrays, that the user can choose to add together or subtract. Right now, im jsut trying to get the user input to work, but im getting a lot of errors. What am i doing wrong?

    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[][] arrayOne = new int[ROWS][COLS];
    		int[][] arrayTwo = new int[ROWS][COLS];
    		int[][] arraySolution = new int[ROWS][COLS];
    		
    		
    		System.out.println("Choose from the menu:" + "\nAdd two arrays: 1" +"\nSubtract two arrays: 2" + "\nExit: 3");
    		menuChoice = keyboard.nextInt();
    		
    		switch(menuChoice)
    		{
    			case 1:
    			{
    				arrayInput(arrayOne, arrayTwo);
    			}
    			case 2:
    			{
    				arrayInput(arrayOne, arrayTwo);
    			}
    			case 3:
    				System.exit(0);
    		}
    	}
    	public static int[][] arrayInput(int[][] arrayOne, int[][] arrayTwo)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		
    		System.out.println("How many rows are there?");
    		ROWS = keyboard.nextInt();
    		
    		System.out.println("How many columns are there?");
    		COLS = keyboard.nextInt();
    		
    		System.out.println("Enter data for first array.");
    		
    		for(int row = 0; row < ROWS; row++)
    		{
    			for(int col = 0; col < COLS; col++)
    			{
    				arrayOne[row][col] = keyboard.nextInt();
    			}
    		}
    		
    		System.out.println("Enter data for second array.");
    		
    		for(int row = 0; row < ROWS; row++)
    		{
    			for(int col = 0; col < COLS; col++)
    			{
    				arrayTwo[row][col] = keyboard.nextInt();
    			}
    		}
    		return arrayOne[row][col];
    		return arrayTwo[row][col];
    	}
    }
    tons of errors :(

    I:\Documents\Spring 2010\Computer Science II\Lab3.java:14: cannot find symbol
    symbol : variable ROWS
    location: class Lab3
    int[][] arrayOne = new int[ROWS][COLS];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:14: cannot find symbol
    symbol : variable COLS
    location: class Lab3
    int[][] arrayOne = new int[ROWS][COLS];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:15: cannot find symbol
    symbol : variable ROWS
    location: class Lab3
    int[][] arrayTwo = new int[ROWS][COLS];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:15: cannot find symbol
    symbol : variable COLS
    location: class Lab3
    int[][] arrayTwo = new int[ROWS][COLS];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:16: cannot find symbol
    symbol : variable ROWS
    location: class Lab3
    int[][] arraySolution = new int[ROWS][COLS];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:16: cannot find symbol
    symbol : variable COLS
    location: class Lab3
    int[][] arraySolution = new int[ROWS][COLS];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:41: cannot find symbol
    symbol : variable ROWS
    location: class Lab3
    ROWS = keyboard.nextInt();
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:44: cannot find symbol
    symbol : variable COLS
    location: class Lab3
    COLS = keyboard.nextInt();
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:48: 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:50: cannot find symbol
    symbol : variable COLS
    location: class Lab3
    for(int col = 0; col < COLS; col++)
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:58: 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:60: cannot find symbol
    symbol : variable COLS
    location: class Lab3
    for(int col = 0; col < COLS; col++)
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:65: cannot find symbol
    symbol : variable row
    location: class Lab3
    return arrayOne[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:65: cannot find symbol
    symbol : variable col
    location: class Lab3
    return arrayOne[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:65: incompatible types
    found : int
    required: int[][]
    return arrayOne[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:66: cannot find symbol
    symbol : variable row
    location: class Lab3
    return arrayTwo[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:66: cannot find symbol
    symbol : variable col
    location: class Lab3
    return arrayTwo[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:66: incompatible types
    found : int
    required: int[][]
    return arrayTwo[row][col];
    ^
    18 errors

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

    Default

    The errors are telling you what's wrong:

    Consider this line:
    Java Code:
        int[][] arrayOne = new int[ROWS][COLS];
    One error states that it can't find the ROWS variable and another states the same for COLS. So look in your code and try to see if and where you declare these variables before you're using them.

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

    Default

    i forgot to declare ROWS, COLS.

    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, COLS = 0;
    		int[][] arrayOne = new int[ROWS][COLS];
    		int[][] arrayTwo = new int[ROWS][COLS];
    		int[][] arraySolution = new int[ROWS][COLS];
    		
    		
    		System.out.println("Choose from the menu:" + "\nAdd two arrays: 1" +"\nSubtract two arrays: 2" + "\nExit: 3");
    		menuChoice = keyboard.nextInt();
    		
    		switch(menuChoice)
    		{
    			case 1:
    			{
    				arrayInput(arrayOne, arrayTwo);
    			}
    			case 2:
    			{
    				arrayInput(arrayOne, arrayTwo);
    			}
    			case 3:
    				System.exit(0);
    		}
    	}
    	public static int[][] arrayInput(int[][] arrayOne, int[][] arrayTwo)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		
    		System.out.println("How many rows are there?");
    		ROWS = keyboard.nextInt();
    		
    		System.out.println("How many columns are there?");
    		COLS = keyboard.nextInt();
    		
    		System.out.println("Enter data for first array.");
    		
    		for(int row = 0; row < ROWS; row++)
    		{
    			for(int col = 0; col < COLS; col++)
    			{
    				arrayOne[row][col] = keyboard.nextInt();
    			}
    		}
    		
    		System.out.println("Enter data for second array.");
    		
    		for(int row = 0; row < ROWS; row++)
    		{
    			for(int col = 0; col < COLS; col++)
    			{
    				arrayTwo[row][col] = keyboard.nextInt();
    			}
    		}
    		return arrayOne[row][col];
    		return arrayTwo[row][col];
    	}
    }
    but i dont understand how i get the size declarators to go into the method, or why the method isnt finding row and col.

    I:\Documents\Spring 2010\Computer Science II\Lab3.java:42: cannot find symbol
    symbol : variable ROWS
    location: class Lab3
    ROWS = keyboard.nextInt();
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:45: cannot find symbol
    symbol : variable COLS
    location: class Lab3
    COLS = keyboard.nextInt();
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:49: 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:51: cannot find symbol
    symbol : variable COLS
    location: class Lab3
    for(int col = 0; col < COLS; col++)
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:59: 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:61: cannot find symbol
    symbol : variable COLS
    location: class Lab3
    for(int col = 0; col < COLS; col++)
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:66: cannot find symbol
    symbol : variable row
    location: class Lab3
    return arrayOne[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:66: cannot find symbol
    symbol : variable col
    location: class Lab3
    return arrayOne[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:66: incompatible types
    found : int
    required: int[][]
    return arrayOne[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:67: cannot find symbol
    symbol : variable row
    location: class Lab3
    return arrayTwo[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:67: cannot find symbol
    symbol : variable col
    location: class Lab3
    return arrayTwo[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:67: incompatible types
    found : int
    required: int[][]
    return arrayTwo[row][col];
    ^
    12 errors

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Since ROWS (a bad name for a variable by the way) is declared within the main method, it is only visible within the main method. Keep studying! :)

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

    Default

    Quote Originally Posted by Fubarable View Post
    Since ROWS (a bad name for a variable by the way) is declared within the main method, it is only visible within the main method. Keep studying! :)
    thats how the book did it. but why are my indexes not being found? the lower case rows, cols in the method?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Meta View Post
    thats how the book did it.
    Please double check the book and see if ROWS and COLS were being used as constants -- were declared final and static. I'll bet you'll find this so, since only constants are supposed to be named in all-caps.

    but why are my indexes not being found? the lower case rows, cols in the method?
    Huh? I told you one of your problems -- ROWS and COLS are declared in the main method and are thus not visible in other methods. You'll have to re-declare them in any methods that need to use them.

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

    Default

    Quote Originally Posted by Fubarable View Post
    Please double check the book and see if ROWS and COLS were being used as constants -- were declared final and static. I'll bet you'll find this so, since only constants are supposed to be named in all-caps.


    Huh? I told you one of your problems -- ROWS and COLS are declared in the main method and are thus not visible in other methods. You'll have to re-declare them in any methods that need to use them.
    they were FINALS. But its a for loop in the method, so im declaring row and col in the for loop, so i dont understand why it says its not found. its right there in the for loop. row = 0; col = 0;

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

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

    Default

    i updated my code. i feel like the point of methods is so you dont have to declare in both methods. make code simpler, but this feels more complicated. anyway heres my updated code, and it cant find row or col when i want to assign it into the 2d array, it also says incompatible types, but i dont know why.

    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, cols = 0;
    		int[][] arrayOne = new int[rows][cols];
    		int[][] arrayTwo = new int[rows][cols];
    		int[][] arraySolution = new int[rows][cols];
    		
    		
    		System.out.println("Choose from the menu:" + "\nAdd two arrays: 1" +"\nSubtract two arrays: 2" + "\nExit: 3");
    		menuChoice = keyboard.nextInt();
    		
    		switch(menuChoice)
    		{
    			case 1:
    			{
    				arrayInput(arrayOne, arrayTwo);
    			}
    			case 2:
    			{
    				arrayInput(arrayOne, arrayTwo);
    			}
    			case 3:
    				System.exit(0);
    		}
    	}
    	public static int[][] arrayInput(int[][] arrayOne, int[][] arrayTwo)
    	{
    		int rows, cols = 0;
    		Scanner keyboard = new Scanner(System.in);
    		
    		System.out.println("How many rows are there?");
    		rows = keyboard.nextInt();
    		
    		System.out.println("How many columns are there?");
    		cols = keyboard.nextInt();
    		
    		System.out.println("Enter data for first array.");
    		
    		for(int row = 0; row < rows; row++)
    		{
    			for(int col = 0; col < cols; col++)
    			{
    				arrayOne[row][col] = keyboard.nextInt();
    			}
    		}
    		
    		System.out.println("Enter data for second array.");
    		
    		for(int row = 0; row < rows; row++)
    		{
    			for(int col = 0; col < cols; col++)
    			{
    				arrayTwo[row][col] = keyboard.nextInt();
    			}
    		}
    		return arrayOne[row][col];
    		return arrayTwo[row][col];
    	}
    }
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:67: cannot find symbol
    symbol : variable row
    location: class Lab3
    return arrayOne[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:67: cannot find symbol
    symbol : variable col
    location: class Lab3
    return arrayOne[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:67: incompatible types
    found : int
    required: int[][]
    return arrayOne[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:68: cannot find symbol
    symbol : variable row
    location: class Lab3
    return arrayTwo[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:68: cannot find symbol
    symbol : variable col
    location: class Lab3
    return arrayTwo[row][col];
    ^
    I:\Documents\Spring 2010\Computer Science II\Lab3.java:68: incompatible types
    found : int
    required: int[][]
    return arrayTwo[row][col];

    ^

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

    Default

    i changed a bunch of stuff. i dont know if im on the right track or not tho..... i ran it, just to see if it would work so far, but i get an arrayoutofbounds :/

    Java Code:
    import java.util.Scanner;
    
    public class Lab3
    {
    	public static void main(String [] args)
    	{
    		Scanner keyboard = new Scanner(System.in);
    		
    		int menuChoice = 0;
    		
    		System.out.println("Choose from the menu:" + "\nAdd two arrays: 1" +"\nSubtract two arrays: 2" + "\nExit: 3");
    		menuChoice = keyboard.nextInt();
    		
    		switch(menuChoice)
    		{
    			case 1:
    			{
    				arrayInput();
    			}
    			case 2:
    			{
    				arrayInput();
    			}
    			case 3:
    				System.exit(0);
    		}
    	}
    	public static void arrayInput()
    	{
    		int rows = 0, cols = 0;
    		int[][] arrayOne = new int[rows][cols];
    		int[][] arrayTwo = new int[rows][cols];
    		Scanner keyboard = new Scanner(System.in);
    		
    		System.out.println("How many rows are there?");
    		rows = keyboard.nextInt();
    		
    		System.out.println("How many columns are there?");
    		cols = keyboard.nextInt();
    		
    		System.out.println("Enter data for first array.");
    		
    		for(int row = 0; row < rows; row++)
    		{
    			for(int col = 0; col < cols; col++)
    			{
    				arrayOne[row][col] = keyboard.nextInt();
    			}
    		}
    		
    		System.out.println("Enter data for second array.");
    		
    		for(int row = 0; row < rows; row++)
    		{
    			for(int col = 0; col < cols; col++)
    			{
    				arrayTwo[row][col] = keyboard.nextInt();
    			}
    		}
    	}
    }

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The order that you do things matters. For instance here:
    Java Code:
    		int rows = 0, cols = 0;
    
    		// what size are the arrays?  How many rows
    		// and how many columns?  (hint: look at the 
    		// rows and cols ints above.
    		int[][] arrayOne = new int[rows][cols];
    		int[][] arrayTwo = new int[rows][cols];
    		Scanner keyboard = new Scanner(System.in);
    		
    
    		// so now you change the rows and cols below, but
    		// will this change the array size?
    		// would it be better to set the array size after this code below
    		// is called?
    		System.out.println("How many rows are there?");
    		rows = keyboard.nextInt();
    		
    		System.out.println("How many columns are there?");
    		cols = keyboard.nextInt();
    Please see my comments in your code above.

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

    Default

    ya. i figured that out. i fixed it. so now the input works. im just not sure how i should go about adding or subtracting the arrays. i thought about calling from inside the method, but that wont work becuase how do i know if the user wants to add or minus? i ask that before the input happens. so im thinking i should make another call to a new method below the input call in the case statements. but do i need to return the arrays from the method i have now, back to main, and then send it to another method. im just confused on this issue.

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

    Default

    It's very important, especially when you're learning, to keep things in small, tight, easy to understand methods. A lot of texts seem to like to start with user input, I guess because it seems like you're really doing something. But in reality, user input from the console is difficult to do correctly, and nobody runs console programs these days anyway.

    Start by writing the method that does the real work. In this case, that means write these methods:
    Java Code:
            public int[][] addMatrix(int[][] matrix1, int[][] matrix2) {
                    ...
            }
    
            public int[][] subtractMatrix(int[][] matrix1, int[][] matrix2) {
                    ...
            }
    Test them with a pair of hard-coded matrices, like this:
    Java Code:
            private void testMatrixMath() {
                    int[][] matrix1 = {{1, 2, 3},
                                       {4, 5, 6}};
                    printMatrix("matrix1:", matrix1);
                    int[][] matrix2 = {{6, 5, 4},
                                       {3, 2, 1}};
                    printMatrix("matrix2:", matrix2);
                    int[][] matrixSum = addMatrix(matrix1, matrix2);
                    printMatrix("matrixSum:", matrixSum);
                    int[][] matrixDifference = subtractMatrix(matrixSum, matrix2);
                    printMatrix("matrixDifference:", matrixDifference);
            }
    
            private void printMatrix(String desc, int[][] matrix) {
                    System.out.println(desc + "{");
                    for (int[] row : matrix) {
                            System.out.print("{");
                            for (int n : row) {
                                    System.out.print("" + n + "  ");
                            }
                            System.out.println("}");
                    }
                    System.out.println("}");
            }
    Get that working, and then worry about doing input. The advantage of this approach is that it gets you to think in terms of data structures -- what's in memory, how it's laid out, and how it gets passed around -- rather than concentrating on presentation. You'll soon start to realize that user input is just another method (interfacing with an annoying and unreliable API that is the Human Being).

    -Gary-

Similar Threads

  1. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  2. Replies: 1
    Last Post: 03-04-2010, 12:19 PM
  3. Replies: 2
    Last Post: 11-18-2008, 11:44 PM
  4. Help needed with java arrays code
    By d24706 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 02:11 AM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 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
  •