# Summing a 4X4 Matrix in JOptionPane

• March 14th, 2011, 09:50 PM
Java Neil
Summing a 4X4 Matrix in JOptionPane
Hi all...I'm a little stuck here. I have an assignment (summing all the numbers in a matrix) that I have figured out using the scanner and I need to figure out a way to turn it into a JOptionPane that "Echos the inputs as a[0] = xx, a[1] = xx, a[3] = xx, a[4] = xx, then displays the reslutl"

This is what I have figured out so far. Please check my logic as well as I'm very new to two-dimensional arrays.

Code java:

``` import java.util.Scanner; public class sumMatrix { public static void main(String[] args) { Scanner input = new Scanner(System.in);   System.out.print("Enter a 4 by 4 matrix row by row:"); int[][] matrix = new int[4][4];   for (int i = 0; i < matrix.length; i++) for (int j = 0; j < matrix[i].length; j++) matrix[i][j] = input.nextInt();   System.out.print("Sum of the Matrix is " + sumMatrix(matrix)); double sum = sumMatrix(matrix); } public static double sumMatrix(int[][] m) { int sum = 0;   for (int i = 0; i < m.length; i++) for (int j = 0; j < m[i].length; j++) sum = sum + m[i][j];   return sum; } }```

Thanks once again for everyone's help. Thanks to you guys, I'm doing alright in this class.
• March 14th, 2011, 10:39 PM
javapenguin
Re: Summing a 4X4 Matrix in JOptionPane
double sum = sumMatrix(matrix);

Not sure what that's for.

Anyway, you might be looking for something like:

Code java:

```import javax.swing.JOptionPane; public class sumMatrix { public static void main(String[] args) {     JOptionPane.showMessageDialog(null, "Enter a 4 by 4 matrix row by row:");   int[][] matrix = new int[4][4];   for (int i = 0; i < matrix.length; i++) for (int j = 0; j < matrix[i].length; j++) matrix[i][j] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter an integer for the matrix.");     JOptionPane.showMessageDialog(null, "Sum of the Matrix is " + sumMatrix(matrix)); // double sum = sumMatrix(matrix); } public static double sumMatrix(int[][] m) { int sum = 0;   for (int i = 0; i < m.length; i++) for (int j = 0; j < m[i].length; j++) sum = sum + m[i][j];   return sum; } }```

JOptionPane (Java Platform SE 6)
• March 14th, 2011, 10:46 PM
Java Neil
Re: Summing a 4X4 Matrix in JOptionPane
Good catch! Thanks...
• March 15th, 2011, 12:48 AM
Java Neil
Re: Summing a 4X4 Matrix in JOptionPane
In thinking about this some more I think I need to store each inputs index and recall that index at the end to "Echo the inputs as a[0] = xx, a[1] = xx, a[3] = xx, a[4] = xx, then displays the reslutl"

maybe something like
Code java:

` [0] = Double.parseDouble(JOptionPane.showInputDialog(null, "1st number" + " "));`
16 times.

What do you think? I just need to echo the input at the end of the program, and I can't think of another way to do it.
• March 15th, 2011, 09:15 AM
Java Neil
Re: Summing a 4X4 Matrix in JOptionPane
Quote:

Originally Posted by Java Neil
In thinking about this some more I think I need to store each inputs index and recall that index at the end to "Echo the inputs as a[0] = xx, a[1] = xx, a[3] = xx, a[4] = xx, then displays the reslutl"

maybe something like
Code java:

` [0] = Double.parseDouble(JOptionPane.showInputDialog(null, "1st number" + " "));`
16 times.

What do you think? I just need to echo the input at the end of the program, and I can't think of another way to do it.

Well I've messed around with this for hours, and I'm pretty sure this is not how to do this...Once again, I need some help storing the user input and echoing this input at the same time as the sum of the matrix is given. Everything I have tried has either caused errors and would not compile, or has caused logical errors.