Results 1 to 13 of 13
Thread: 2d Arrays Code help!
- 04-04-2010, 02:48 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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?
tons of errors :(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]; } }
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
-
The errors are telling you what's wrong:
Consider this line:
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.Java Code:int[][] arrayOne = new int[ROWS][COLS];
- 04-04-2010, 02:57 AM #3
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
i forgot to declare ROWS, COLS.
but i dont understand how i get the size declarators to go into the method, or why the method isnt finding row and col.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]; } }
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
-
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! :)
- 04-04-2010, 03:03 AM #5
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
-
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.but why are my indexes not being found? the lower case rows, cols in the method?
- 04-04-2010, 03:11 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
-
Again, in that method (the arrayInput method), where do you declare ROWS or COLS before you use them?
- 04-04-2010, 03:26 AM #9
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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.
I:\Documents\Spring 2010\Computer Science II\Lab3.java:67: cannot find symbolJava 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]; } }
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];
^
- 04-04-2010, 03:48 AM #10
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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(); } } } }
-
The order that you do things matters. For instance here:
Please see my comments in your code above.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();
- 04-04-2010, 04:10 AM #12
Member
- Join Date
- Mar 2010
- Posts
- 78
- Rep Power
- 0
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.
- 04-04-2010, 06:49 AM #13
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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:
Test them with a pair of hard-coded matrices, like this:Java Code:public int[][] addMatrix(int[][] matrix1, int[][] matrix2) { ... } public int[][] subtractMatrix(int[][] matrix1, int[][] matrix2) { ... }
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).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("}"); }
-Gary-
Similar Threads
-
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
can any one pls send me a sample code for calling a jsp code in swings
By sniffer139 in forum AWT / SwingReplies: 1Last Post: 03-04-2010, 11:19 AM -
Can someone please help fix this code so the program works? (2D arrays)
By busdude in forum New To JavaReplies: 2Last Post: 11-18-2008, 10:44 PM -
Help needed with java arrays code
By d24706 in forum New To JavaReplies: 2Last Post: 03-07-2008, 01:11 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks