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.

Results 1 to 5 of 5

Thread: Summing a 4X4 Matrix in JOptionPane

  1. #1
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

       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.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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[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)

  3. #3
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Summing a 4X4 Matrix in JOptionPane

    Good catch! Thanks...

  4. #4
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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
     [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.

  5. #5
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Summing a 4X4 Matrix in JOptionPane

    Quote Originally Posted by Java Neil View Post
    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
     [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.

    Please help!

Similar Threads

  1. Matrix Program
    By panzer in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 30th, 2013, 06:15 AM
  2. Having problem in matrix multiplication....
    By sidhant in forum Java Theory & Questions
    Replies: 5
    Last Post: March 17th, 2011, 01:41 PM
  3. Adding two matrix together
    By papated21 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 2nd, 2011, 01:52 PM
  4. Im so confused! Counting and Summing
    By captiancd89 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 28th, 2010, 06:49 PM
  5. Embedding images in a matrix.
    By Aims_ in forum Java Theory & Questions
    Replies: 1
    Last Post: September 11th, 2009, 02:37 PM