i have written a code to out put the result of multiplication of two matrices using joption frame, but it outputs the result on separate windows, dont know how to output them on the same window.
this is my code:

    'public class matrixmul {
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package javaapplication1;
    import javax.swing.JOptionPane;

    /**
     *
     * @author CHARLES
     */
    public class matrixmul {
         public static void main(String[] args) {
             String as, bs, muls, rowas, colas, rowbs, colbs,p="  \n\n"; 
        int a[][]= new int[10][10];
        int b[][]= new int[10][10];
        int mul[][]= new int [10][10];
        int rowa, cola,rowb, colb, i, j, k;

        rowas= JOptionPane.showInputDialog("enter the number of rows for matrix A");
        rowa= Integer.parseInt(rowas);
        colas= JOptionPane.showInputDialog("enter the number of columns for matrix A");
        cola= Integer.parseInt(colas);
        rowbs=JOptionPane.showInputDialog("enter the number of columns for matrix B");
        rowb= Integer.parseInt(rowbs);
        colbs=JOptionPane.showInputDialog("enter the number of columns for matrix B");
        colb=Integer.parseInt(colbs);
        if (rowa==colb){
            JOptionPane.showMessageDialog(null,"the operation is feasible, press the enter key to proceed!","operation test result",
            JOptionPane.PLAIN_MESSAGE);
            for(i=0;i<rowa;i++){
                for(j=0;j<cola;j++){
                    as=JOptionPane.showInputDialog("enter the elements of matrix A ");
                    a[i][j]=Integer.parseInt(as);

                }



                }
            for(i=0;i<rowb;i++){
                for(j=0;j<colb;j++){
                    bs=JOptionPane.showInputDialog("enter the elements of matrix B /n");
                    b[i][j]=Integer.parseInt(bs);

                }
            }
            for (i=0;i<rowa;i++){
                for(j=0;j<colb;j++){
                    for(k=0;k<rowa;k++){
                        mul[i][j] = mul[i][j] + a[i][k] * b[k][j];


                    }
                }
            }
            for(i=0;i<rowa;i++){
                for(j=0;j<colb;j++){

                    JOptionPane.showMessageDialog(null," the resultant matrix is"+mul[i][j]+"\n","result",JOptionPane.PLAIN_MESSAGE);




            }








        }







    }
        System.exit(0);
    }
    }'

i just want to know how to output all the elements of the resultant matrix on a single window using joptionpane

apart from the typo's in code , which you can correcteasily by pasting it into an IDE , look at your last for() loop

        for (i = 0; i < rowa; i++) {
            for (j = 0; j < colb; j++) {

                    JOptionPane.showMessageDialog(null,
                            " the resultant matrix is" + mul[i][j] + "\n",
                            "result", JOptionPane.PLAIN_MESSAGE);

            }

        }

it creates a new window for each entry in your result matrix.

what you have to do, is create an empty String before you iterate over your arrays.
during the iteration (for loops) you append data to it.
after the iteration, you show one single messageDialog, with the String you just generated as text.

and while your at it as stultuske suggests , would be good if you can learn about StringBuilders too. google it up.

Edited 3 Years Ago by somjit{}

This article has been dead for over six months. Start a new discussion instead.