2

I have 2 text files called matrix1 and matrix 2. they have numbers stacked in 4 rows and 4 columns. How can I read it and display it using JOptionPane? The problem is on the showTable method i believe.

public class MainApp {

private static final int ROW = 4;
private static final int COL = 4;

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    double[][] m1 = new double[ROW][COL];
    double[][] m2 = new double[ROW][COL];
    double[][] m3 = new double[ROW][COL];

    double[][] m4 = new double[COL][ROW];


    m1 = LeerDatos("matrix1.txt");

    showTable("Tabla 1", m1);

    m2 = LeerDatos("matrix2.txt");

    showTable("Tabla 2", m2);

    m3 = sumarDatos(m1, m2);
    showTable("m1 + m2: ", m3);
    salvarTabla("suma.txt", m3);

    m3 = restarTabla(m1,m2);

    showTable("m1 - m2: ", m3);
    salvarTabla("resta.txt", m3);

    String numeroString = JOptionPane.showInputDialog("Escriba el numero escalar: ", "0.0");

    double scalar = Double.parseDouble(numeroString);

    m3 = scalarMultiplication( scalar, m2);

    showTable("multiplicacion escalar con "
            + scalar , m3);
    salvarTabla("scalar.txt", m3);

    m4 = transpuesta(m1);

    showTable("La transpuesta de tabla 1: ", m4);
    salvarTabla("transpuesta.txt", m4);

    System.exit(0);

}//main

private static void salvarTabla(String string, double[][] m4) {


}

private static double[][] transpuesta(double[][] m1) {


    double[][] dummies = new double[m1.length][m1[0].length];
            for (int row = 0; row < dummies.length; row++) {
                for (int col = 0; col < dummies.length; col++) {

                    dummies[col][row] = m1[row][col]; 

                }

            }
    return dummies;
}

private static double[][] scalarMultiplication(double scalar, double[][] m2) {

    return null;
}

private static double[][] restarTabla(double[][] m1, double[][] m2) {


    double dummies[][] = new double[m1.length][m1[0].length];

    for (int row = 0; row < dummies.length; row++) {
        for (int col = 0; col < dummies.length; col++) {
            dummies[row][col] = m1[row][col] - m2[row][col];

        }// for de col

    }// for de row


    return dummies;
}

private static double[][] sumarDatos(double[][] m1, double[][] m2) {

    double dummies[][] = new double[m1.length][m1[0].length];

    for (int row = 0; row < dummies.length; row++) {
        for (int col = 0; col < dummies.length; col++) {
            dummies[row][col] = m1[row][col] + m2[row][col];

        }// for de col

    }// for de row


    return dummies;
}//sumar tabla 

private static void showTable(String string, double[][] m1) {
    // TODO Auto-generated method stub
    double[][] dummies = new double[m1.length][m1[0].length];
    for (int row = 0; row < dummies.length; row ++) {
        for (int col = 0; col < dummies.length; col++) {

            dummies[row][col] = m1[row][col];
            JOptionPane.showMessageDialog(null, m1[row][col]);
        }

    }



}

private static double[][] LeerDatos(String filename) {


    File file = new File(filename);


    double dummies[][] = new double[ROW][COL];  
    try {
        Scanner scanner = new Scanner ( new File (filename));

        for (int row = 0; row < dummies.length; row++) {

            for (int col = 0; col < dummies.length; col++) {

                dummies[row][col] = scanner.nextDouble();
            }

            }// inner loop


    } catch (FileNotFoundException e) {

        e.printStackTrace();


}
    return dummies;


}

}
DT7
  • 1,615
  • 14
  • 26
  • 2
    And what **is** the problem? Are you getting any error messages? Something else? – PM 77-1 Oct 09 '13 at 18:52
  • Build the results into a HTML table, as shown in [this example](http://stackoverflow.com/questions/15940485/formatting-the-output-of-arrays-into-a-column/15940611#15940611) and [this example](http://stackoverflow.com/questions/15824033/joptionpane-multidimensional-array-output/15824067#15824067) – MadProgrammer Oct 09 '13 at 20:26
  • @MadProgrammer i linked too xD i didnt see it – nachokk Oct 09 '13 at 20:35
  • @nachokk Great minds ;) – MadProgrammer Oct 09 '13 at 20:41

1 Answers1

2

You can use this method Arrays#deepToString(Object[]), and you avoid using the "for loops".

JOptionPane.showMessageDialog(null, Arrays.deepToString(m1));

Example :

public static void main(String[] args) {
      int [][] numeros = new int[3][2]; 
      JOptionPane.showMessageDialog(null, Arrays.deepToString(numeros));  

 }

Output:

enter image description here

UPDATE

As you want to make something like a table. You have 2 options make your own jtable and add it to the JOptionPane or as seen in this answer using html

Example SSCCE:

public class Text{

  public static void main(String[] args) {
        int[][] numeros = new int[4][4];
        int i = 0, j = 0;
        StringBuilder sb = new StringBuilder(64);
        sb.append("<html><table><tr><td>Col1</td><td>Col2</td><td>Col3</td><td>Col4</td></tr>");
        for (i = 0; i < 4; i++) {
            sb.append("<tr>");
            for (j = 0; j < 4; j++) {
                sb.append("<td> ").append(numeros[i][j]).append("</td>");
            }
            sb.append("</tr>");
        }
        sb.append("</table></html>");
        JOptionPane.showMessageDialog(null, sb);
  }


}

And the output:

enter image description here

Community
  • 1
  • 1
nachokk
  • 14,363
  • 4
  • 24
  • 53