Search code examples
javaarraysjoptionpane

Java - 2 dimensional array in JOptionPane how to show it all at once


I have a two dimensional array, which i want to show via a JOptionPane. So far it is showing me 1 row at a time. But i would like it to show all 8 rows at once.

It is also showing brackets and comma's in the JOptionPane once i run the code. Is there some way to get rid of those brackets and comma's?

This is my code so far, im just started learning Java.

package indzendopgave2;
import javax.swing.JOptionPane;

import java.util.Arrays;

public class Inzend2 {

public static void main(String[] args) {
    //Creating array
    int[][] blastTable = new int[][]{
            {32,31,30,29,28,27,26,25},
            {24,23,22,21,20,19,18,17},
            {16,15,14,13,12,11,10,9},
            {8,7,6,5,4,3,2,1},
            {0,-1,-2,-3,-4,-5,-6,-7},
            {-8,-9,-10,-11,-12,-13,-14,-15},
            {-16,-17,-18,-19,-20,-21,-22,-23},
            {-24,-25,-26,-27,-28,-29,-30,-31}
    };

    printArray(blastTable);
}



//Method to print two dimensional array in a JOptionPane
public static void printArray(int[][] num1){

    for(int x=0; x<num1.length; x++){
        String output = Arrays.toString(num1[x]);
            JOptionPane.showMessageDialog(null, output, "Uitvoer",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }
}

Solution

  • This will also do

      public static void printArray(int[][] num1) {
        String output = "";
        for (int x = 0; x < num1.length; x++) {
            output += Arrays.toString(num1[x]) + "\n";
        }
        JOptionPane.showMessageDialog(null, output, "Uitvoer",
                JOptionPane.INFORMATION_MESSAGE);
    }