-1

Wen I have this code:

    import java.util.Scanner;
    import java.util.Arrays;
    public class Ex02 {

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            int [] matrixSize = new int [4];
            System.out.println("Insert the values matrix (matrixA_lines,MatrixA_Rows,matrixB_lines,MatrixB_Rows");
            matrixSize = matrixFill(4);
            System.out.println(Arrays.toString(matrixSize));
        }

        public static int[] matrixFill(int sizeOne){
            int i;
            Scanner sc = new Scanner(System.in);

            int [] matrixTemp = new int [sizeOne];
            for (i = 0; i<sizeOne; i++){
                matrixTemp[i] = sc.nextInt();
            }

            return matrixTemp;
        }
    }

It all works as expected. An unidimensional array is created, filled with 1,2,3,4 and them the array is print. The problem is that i want to use an bidimentsional array. I've modified the code and it gives error. Here is the modified code:

import java.util.Scanner;
import java.util.Arrays;
public class Ex02 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int [][] matrixSize = new int [1][4];
        System.out.println("Insert the values matrix (matrixA_lines,MatrixA_Rows,matrixB_lines,MatrixB_Rows");
        matrixSize[][] = matrixFill(1,4);
        System.out.println(Arrays.deepToString(matrixSize));
    }

    public static int[][] matrixFill(int sizeOne, int sizeTwo){
        int i;
        Scanner sc = new Scanner(System.in);

        int [][] matrixTemp = new int [sizeOne][sizeTwo];
        for (i = 0; i<sizeOne; i++){
            matrixTemp[0][i] = sc.nextInt();
        }

        return matrixTemp[sizeOne][sizeTwo];
    }
}

On line 21 (matrixSize[][] = matrixFill(1,4);) the error is:

cannot find symbol
symbol:   class matrixSize
location: class Ex02.Ex02

not a statement

';' expected

And on line 34(return matrixTemp[sizeOne][sizeTwo];) the error is:

incompatible types
required: int[][]
found:    int

Can someone tell me what I'm doing wrong? Just started to learn Java.

Regards,

favolas

3
  • The return statement 'return matrixTemp[sizeOne][sizeTwo]' is returning the object found at [sizeOne][sizeTwo] and so it fails since the object found there is only a int, since you declare that the method should return something that is int[][]. 'return matrixTemp' will do fine there. Commented Sep 28, 2011 at 22:24
  • The first error, you should only put matrixSize = matrixFill(1,4) since you already declared what matrixSize is. Commented Sep 28, 2011 at 22:25
  • apart from the type issue, the expression to the return statement would throw an out of bounds error if the indexing were actually attempted. Commented Oct 10 at 17:09

3 Answers 3

2

Remove the [][] from matrixSize, and from your return value.

matrixSize = matrixFill(1,4);

And

return matrixTemp;
Sign up to request clarification or add additional context in comments.

2 Comments

Just a note... I don't have line numbers on the code you provided, but I'll bet they're 21 and 34. Just saying, those compilation errors will help you a lot. Learning to read your tools' outputs will help you be a better programmer. Forever. :)
Hi there. I've stated that the errors were on those lines. Even with the compilation errors I wasn't getting it right.
0
matrixSize[][] = matrixFill(1,4);

That's not a valid place for the [][]. The first example had it right:

matrixSize = matrixFill(1,4);

Comments

0

You added "[sizeOne][sizeTwo]" to the return statement in the new code; you do not need this because matrixTemp has already been declared as a 2d array. The way you changed it, you are sending one element within the array.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.