如何读取文本文件并使用JOptionPane显示它。 (2d数组)

时间:2013-10-09 18:49:48

标签: java multidimensional-array joptionpane

我有两个名为matrix1和matrix 2的文本文件。它们的数字分为4行和4列。如何阅读并使用JOptionPane显示它?问题在于我认为的showTable方法。

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;


}

}

1 个答案:

答案 0 :(得分:2)

您可以使用此方法Arrays#deepToString(Object[]),并避免使用 “for loops”。

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

示例:

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

 }

输出:

enter image description here

<强>更新

因为你想做一个像桌子的东西。您有2个选项制作自己的jtable并将其添加到JOptionPane或如此答案中所示using html

示例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);
  }


}

输出:

enter image description here