Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Summing a 4X4 Matrix in JOptionPane

1. ## 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 = xx, a = xx, a = xx, a = 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.

```   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;

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.  Reply With Quote

3. ## 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:

```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;

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)  Reply With Quote

4. ## Re: Summing a 4X4 Matrix in JOptionPane

Good catch! Thanks...  Reply With Quote

5. ## 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 = xx, a = xx, a = xx, a = xx, then displays the reslutl"

maybe something like
`  = 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.  Reply With Quote

6. ## Re: Summing a 4X4 Matrix in JOptionPane 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 = xx, a = xx, a = xx, a = xx, then displays the reslutl"

maybe something like
`  = 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.

Please help!  Reply With Quote