views:

33

answers:

1

Hello,

I'm having a little difficulty to print a matrix array on dialog box. The matrix is integer and as far as i understood i need to change it into string?

anyway, here's the code:

    public void print_Matrix(int row, int column)
 {

  for (int i = 0; i <= row; i++)


  {
   for (int j = 0; j <= column; j++)
   {
    JOptionPane.showMessageDialog(null, matrix_Of_Life);
   }
  }

what I need to do in order to print array into dialog box?

thanks.

+2  A: 

For small 2D arrays, something like this is convenient:

int[][] matrix = {{1,2,3}, {4,5,6}, {7,8,9}};
String s = Arrays.deepToString(matrix)
   .replace("], ", "\n").replaceAll(",|\\[|\\]", "");

System.out.println(s);

This prints:

1 2 3
4 5 6
7 8 9

This concedes control and speed for clarity and conciseness. If your matrix is larger and/or you want complete control on how each element is printed (e.g. right alignment), you'd probably have to do something else.

polygenelubricants