-2

I've a problem when convert a 2D arrays into an integer using "for each" loop

import java.util.Scanner;

public class matrix{ public static void main(String[] args) {

    Scanner userInput = new Scanner(System.in);

int [][] matrix = new int[2][2];

    for (int x = 0 ; x < 2 ; x++){
        for(int y = 0 ; y < 2 ; y++){
            System.out.printf("enter a number for row %d column %d : ",(x+1),(y+1));
            matrix[x][y] = userInput.nextInt();
        }
    }

for(int t : matrix){
        System.out.print(t + " ");
    }

    userInput.close();
}

}

Yet I get an error int[] cannot be converted to int.

  • For a question like this, you need to provide potential input and corresponding expected output. – Derek Pendleton May 22 '19 at 17:52
  • 1
    Always use proper formatting for your code and specify the question in detail. Mention what is the issue you are facing and just as @DerekPendleton mentioned, The output you are expecting must be properly mentioned. – Gaurav Sharma May 22 '19 at 18:07

1 Answers1

0

Please provide example input and out as @derek-pendleton mentioned.

What you seem to being doing in you for loop is adding rows to your line.

so if your 2D array look like this [ [1, 2], [3, 4] ]. Then int t : matrix, your t would be list namely either: [1, 2] and next iteration [3, 4]. So you need to add another loop in there that says:

for (int[] t : matrix){
    for (int nr : t){
        //print(nr + something);
    }
}