Wesley Harrison
Wesley Harrison

Reputation: 79

cannot convert int to int[] and operator is undefined for types int[], int []

I am having issues printing out my array as my loops will not print due to it saying cannot convert int to int[]. and my addition and multiplication wont concatenate as it says the operator "+" and "*" are undefined for types int[],int[]

If someone could help me figure out why these issues are happening as I though I had it written correctly. IM not very knowledgeable with arrays so any information would be greatly appreciated

import java.util.*;

public class Matrix {
    
    public static void main (String[] args) {
        System.out.println("Please enter number of rows");
        Scanner sc = new Scanner(System.in);
        int row = sc.nextInt();
        System.out.println("Please enter number of columns");
        int column = sc.nextInt();
        
        int[][] a1 = new int[row][column];
        int[][] a2 = new int[row][column];
        
        for(int r = 0; r < row; r++) {
            for(int c = 0; c < column; c++) {
                System.out.println(String.format("Enter first [%d][%d] integer",r,c));


                a1[r]= sc.nextInt();    //cannot convert int to int[]


            }
        }
        
        for(int r = 0; r < row; r++) {
            for(int c = 0; c < column; c++) {
                System.out.println(String.format("Enter first [%d][%d] integer",r,c));


                a2[r]= sc.nextInt();    cannot convert int to int[]


            }
        }

        System.out.println("First Matrix: ");
        PrintArray(a1);
        System.out.println("First Matrix: ");
        PrintArray(a2);
        
        add(a1,a2);
        multiply(a1,a2);
        
        
        }
    
    public static void add(int[][] a1,int[][] a2) {
        int row = a1.length;
        int column = a1[0].length;
        int[][] add = new int [row][column];
        
        for(int r = 0; r < row; r++) {
            for(int c = 0; c < column; c++) {


                add[r] = (a1[r] + a2[r]); // operator "+" is undefined for types int[],int[]


            }
        }
        System.out.println("Sum of added arrays: "+ add);
    }
    
    public static void multiply(int[][] a1,int[][] a2) {
        int row = a1.length;
        int column = a1[0].length;
        int[][] add = new int [row][column];
        
        for(int r = 0; r < row; r++) {
            for(int c = 0; c < column; c++) {


                add[r] = (a1[r] * a2[r]); // operator "*" is undefined for types int[],int[]


            }
        }
        System.out.println("Sum of added arrays: "+ add);
    }
    
    public static void PrintArray(int[][] matrix) {
        for(int r = 0; r < matrix.length; r++ ) {
            for(int c = 0; c <matrix[0].length; c++) {
                System.out.println(matrix[r] + "\t" );
            }
            System.out.println();
        }
    }
}

Upvotes: 0

Views: 160

Answers (2)

Sonia Jain
Sonia Jain

Reputation: 153

In your example, a1 and a2 are 2 dimensional arrays.

Your for loop iteration is fine, but a1[r] is an int[] and not int.

You are trying to assign an integer value to an array, so you are getting a cannot convert error.

a1[r]= sc.nextInt();

Should be changed to:

a1[r][c] = sc.nextInt(); 

Similarly addition and multiplication are only available for the primitive type or wrappers but not on arrays.

add[r] = (a1[r] + a2[r]);

Has to be changed to:

add[r][c] = (a1[r][c] + a2[r][c]);

And

add[r] = (a1[r] * a2[r]);

Has to be changed to:

add[r][c] = (a1[r][c] * a2[r][c]);

Upvotes: 2

smowgli
smowgli

Reputation: 118

you have to put this code then it works

your code is fine you just forgot to put an array with two dimensions a[r][c]

import java.util.*;

public class Matrix {

public static void main (String[] args) {
    System.out.println("Please enter number of rows");
    Scanner sc = new Scanner(System.in);
    int row = sc.nextInt();
    System.out.println("Please enter number of columns");
    int column = sc.nextInt();
    
    int[][] a1 = new int[row][column];
    int[][] a2 = new int[row][column];
    
    for(int r = 0; r < row; r++) {
        for(int c = 0; c < column; c++) {
            System.out.println(String.format("Enter first [%d][%d] integer",r,c));


            a1[r][c]= sc.nextInt();    // 2 dimensions a1[r] is not fine


        }
    }
    
    for(int r = 0; r < row; r++) {
        for(int c = 0; c < column; c++) {
            System.out.println(String.format("Enter first [%d][%d] integer",r,c));


            a2[r][c]= sc.nextInt();    // 2 dimensions a2[r] is not fine


        }
    }

    System.out.println("First Matrix: ");
    PrintArray(a1);
    System.out.println("First Matrix: ");
    PrintArray(a2);
    
    add(a1,a2);
    multiply(a1,a2);
    
    
    }

public static void add(int[][] a1,int[][] a2) {
    int row = a1.length;
    int column = a1[0].length;
    int[][] add = new int [row][column];
    
    for(int r = 0; r < row; r++) {
        for(int c = 0; c < column; c++) {


            add[r][c] = (a1[r][c] + a2[r][c]); // operator "+" is undefined for types int[],int[]


        }
    }
    System.out.println("Sum of added arrays: "+ add);
}

public static void multiply(int[][] a1,int[][] a2) {
    int row = a1.length;
    int column = a1[0].length;
    int[][] add = new int [row][column];
    
    for(int r = 0; r < row; r++) {
        for(int c = 0; c < column; c++) {


            add[r][c] = (a1[r][c] * a2[r][c]); // operator "*" is undefined for types int[],int[]


        }
    }
    System.out.println("Sum of added arrays: "+ add);
}

public static void PrintArray(int[][] matrix) {
    for(int r = 0; r < matrix.length; r++ ) {
        for(int c = 0; c <matrix[0].length; c++) {
            System.out.println(matrix[r] + "\t" );
        }
        System.out.println();
    }
}
}

your output for 2 row 2 column for example

Please enter number of rows
2
Please enter number of columns
2
Enter first [0][0] integer
12
Enter first [0][1] integer
36
Enter first [1][0] integer
25
Enter first [1][1] integer
23

Upvotes: 2

Related Questions