0

Got this error

public class FindError { public static void main(String[] args) { int[][] matrix = {{1}, {2}, {3}, {4}, {5}, {6}}; System.out.println(m(matrix, 1)); //error: incompatible types: int[][] cannot be converted to int } public static int m(int matrix, int column) { int sum = 0; for (int i = 0; i < matrix.length; i++) //error: int cannot be dereferenced sum += matrix[i][column]; //error: array required, but int found return sum; } }

30th Oct 2016, 4:44 PM
frhnims
12 odpowiedzi
0
in public static int m the parameter matrix you must change it in int matrix[ ]
30th Oct 2016, 5:17 PM
Leo Flame
Leo Flame - avatar
0
so public static int m(int matrix [ ] [ ], int column)
30th Oct 2016, 5:18 PM
Leo Flame
Leo Flame - avatar
0
Thank you, So how about the int[][]cannot be converted to int error?
30th Oct 2016, 5:25 PM
frhnims
0
because only int is a variable that stores a number, int [ ] [ ] is a matrix with many numbers, so in the function you wrote int matrix but matrix isn't an int variable, it is an int [ ] [ ] variable so you pass all numbers that it stores
30th Oct 2016, 5:33 PM
Leo Flame
Leo Flame - avatar
0
Sorry so what should i change?
30th Oct 2016, 5:55 PM
frhnims
0
the type of first parameter to int matrix [ ] [ ]
30th Oct 2016, 6:01 PM
Leo Flame
Leo Flame - avatar
0
It show the same error:(
30th Oct 2016, 6:12 PM
frhnims
0
public class FindError { public static void main(String[] args) { int[][] matrix = {{1}, {2}, {3}, {4}, {5}, {6}}; System.out.println(m(matrix, 6)); } public static int m(int matrix [][], int row) { int sum = 0; for (int i = 0; i < row ; i++) sum += matrix[i][0]; return sum; } } try this
30th Oct 2016, 6:20 PM
Leo Flame
Leo Flame - avatar
0
I changed the "column" to "row" because the matrix is 1 2 3 4 5 6 (6 row, 1 column) we must do the sum of this numbers, the variable i at beginning stores 0; this indicate the row that start from 0 to 5 (row-1) so sum+=matrix[i](the row) [0] (because it has only 1 column) after the for loop the function return sum=21
30th Oct 2016, 6:26 PM
Leo Flame
Leo Flame - avatar
0
Get it omg thank you thank you now i understand it thank you again may god bless you leo flameee:)))))
30th Oct 2016, 6:37 PM
frhnims
0
you're welcome :D
30th Oct 2016, 6:48 PM
Leo Flame
Leo Flame - avatar
0
Java has two different types of variables: primitive and objects and only objects are reference types. The type int is a primitive and not an object. Dereferencing is the process of accessing the value referred to by a reference . Since, int is already a value (not a reference), it can not be dereferenced. Primitives (byte, char, short, int, long, float, double, boolean) are not objects and do not have member variables or methods. They're just simple values . So you cannot do somePrimitive.something(). http://net-informations.com/java/err/dereferenced.htm
3rd Dec 2019, 4:30 AM
rahul kumar
rahul kumar - avatar