java - 如何读取文本文件并使用 JOptionPane 显示它。 (二维数组)

标签 java multidimensional-array joptionpane

我有 2 个文本文件,分别称为矩阵 1 和矩阵 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;


}

}

最佳答案

可以使用这个方法Arrays#deepToString(Object[]) ,并且您避免使用 “for 循环”。

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

更新

因为你想要制作像 table 这样的东西。您有 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

关于java - 如何读取文本文件并使用 JOptionPane 显示它。 (二维数组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19280224/

相关文章:

java - Java 生成和垃圾收集器概念的澄清?

Java Hibernate Spring - 具有默认实现的服务接口(interface)

python - 在spark中创建一个多维随机矩阵

Java Swing - 如何在任何 JOptionPane 之前发出蜂鸣声?

java - 在 Android 中使用 Script Engine Manager 评估中缀表达式

java - 了解hibernate缓存

java - 使用嵌套 for 循环修改二维数组

python - 将 2D 数组转换为 3D numpy 数组

java - 使用 JOPtionPane 时遇到问题

java - 如何阻止 Java Swing JOptionPane OK_CANCEL_OPTION 在 OK 选项上关闭?