as above.
e.g
int[] array = new int[can be any size]

joptionpane.showmessagebox(null,
"what to put here is the question"
"messageboxtitle"
joptionpane.informationmessage};
//dont correct the message box crap i was in a rush i know how to do it

e.g
array 1 = 1
array 2 = 5
array 3 = 3
array 4 = 2

the array can be any size thats the problem so i can't just put
"array 1"+ array[0]
"array 2"+ array[1] etc

i can use a for loop when outputting it in a dos window but what can you use for a message box?

Dani AI

Generated

is right that looping with a dialog per element works, but it is hard on users. points you toward aggregating the output into one message, which is the friendlier UX. When the array can be any size, also consider showing a scrollable component so long lists do not overflow the dialog.

Example for a 1D array with labels, using a scrollable, monospaced view:

int[] p = {1, 5, 3, 2}; // your array

StringBuilder sb = new StringBuilder();
for (int i = 0; i < p.length; i++) {
    sb.append("array ").append(i + 1).append(" = ").append(p[i]).append('\n');
}

javax.swing.JTextArea ta = new javax.swing.JTextArea(sb.toString(),
        Math.min(p.length + 1, 20), 24);
ta.setEditable(false);
ta.setFont(new java.awt.Font(java.awt.Font.MONOSPACED, java.awt.Font.PLAIN, 12));

javax.swing.JOptionPane.showMessageDialog(
        null, new javax.swing.JScrollPane(ta), "Printing results",
        javax.swing.JOptionPane.INFORMATION_MESSAGE
);

For a 2D array, print one row per line. This handles jagged rows cleanly:

int[][] m = { {1, 2}, {5, 3, 9}, {2} };

StringBuilder sb = new StringBuilder();
for (int r = 0; r < m.length; r++) {
    sb.append("row ").append(r + 1).append(": ");
    for (int c = 0; c < m[r].length; c++) {
        if (c > 0) sb.append(' ');
        sb.append(m[r][c]);
    }
    sb.append('\n');
}

// reuse the same JTextArea/JScrollPane approach as above to display sb.toString()

Tips:

  • For quick one-liners, java.util.Arrays.toString(p) and java.util.Arrays.deepToString(m) produce readable output.
  • If arrays get large, a JTable in a JScrollPane is more usable than a plain text dump.

Recommended Answers

All 3 Replies

Hi,
Here is a small example how to print an array in Message Box
suppose p is an array:

 for( int i = 0;i < p.length; i++)
    { JOptionPane.showMessageDialog(null, p[i], "Printing 
      results",JOptionPane.INFORMATION_MESSAGE);
   }

Hopefully this would help and good luck.
Dounia

that is likely not what was intended, as it shows a separate messagebox for each element of the array :)
better would be something like

StringBuilder builder = new StringBuilder(p.length);
for (int i=0;i<p.length;builder.append(p[i++])) builder.append("\n");
JOptionPane.showMessageDialog(null, builder.toString(), "Printing results", JOptionPane.INFORMATION_MESSAGE);

this will put each element of the array onto its own line.

When using Java versions lower than 1.5 use StringBuffer instead of StringBuilder.

that is likely not what was intended, as it shows a separate messagebox for each element of the array :)
better would be something like

StringBuilder builder = new StringBuilder(p.length);
for (int i=0;i<p.length;builder.append(p[i++])) builder.append("\n");
JOptionPane.showMessageDialog(null, builder.toString(), "Printing results", JOptionPane.INFORMATION_MESSAGE);

this will put each element of the array onto its own line.

When using Java versions lower than 1.5 use StringBuffer instead of StringBuilder.

How would you to the same to a two dimensional array?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.