0

For example, I have an array of type int and size 3, it has elements 1, 2, 3 When I try to print it in JOptionPane with loops, it makes three different panes.

When I try:

JOptionPane.showMessageDialog( null, array );

It gives garbage values.

I have searched everywhere but I couldn't find a solution. How do I display the array in an option pane?

Tim B
  • 40,716
  • 16
  • 83
  • 128
ahmedbatty
  • 29
  • 1
  • 3
  • 9
  • 1
    That's not the garbage value . You get that value because java `Object` class has `toString` method like this : `public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }` – gtiwari333 Jun 03 '12 at 10:12

4 Answers4

5
// Wrap the list in a JScrollPane if 'size matters'.
JOptionPane.showMessageDialog(null, new JList(array));

E.G.

Using a JList

import javax.swing.*;

public class ArrayDisplay {

    public static void main(String[] args) {
        final String[] array = {
                "JList",
                "JTable for 2D array",
                "HTML in JLabel",
                "Delimited String in JLabel"
        };
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JOptionPane.showMessageDialog(null, new JList(array));
            }
        });
    }
}

(In regard to array toString())

It gives garbage values.

Obscure, yes - garbage, no. AFAIU it is the reference to the array in memory.

..I have an array of type int

Note that a JList array constructor requires objects, so it would need Integer rather than int. To convert from int[] to Integer[] do this:

import javax.swing.*;

public class ArrayDisplay {

    public static void main(String[] args) {
        int[] arrayPrimitive = {1,2,3};
        final Integer[] array = new Integer[arrayPrimitive.length];
        for (int ii=0; ii<arrayPrimitive.length; ii++) {
            array[ii] = arrayPrimitive[ii];
        }
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                JOptionPane.showMessageDialog( null, new JList(array) );
            }
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
3

Try using Arrays.toString():

JOptionPane.showMessageDialog( null, Arrays.toString(array));

The default toString() method for arrays in Java prints, as you called it, garbage values.

See also

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
2

You can try this:

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

Reason for using StringBuilder is that it won't make different panes for you. If you simply go for printing one String at a time, you are bound to get different panes.

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
  • and if I want to print two arrays in columns? – ahmedbatty Jun 03 '12 at 10:15
  • See the example on [this](http://stackoverflow.com/questions/1399281/how-to-print-multiple-arrays-of-different-types-in-java) page to get an idea about printing multiple arrays. – Kazekage Gaara Jun 03 '12 at 10:22
  • You will have to run different `for` loops for each array,appending into the `StringBuilder` for each array one at a time and then printing them. – Kazekage Gaara Jun 03 '12 at 10:23
2

Use Arrays.toString () as :

    int [] arr = new int [3];
    arr[0]=1;
    arr[1]=2;
    arr[2]=3;

    JOptionPane.showMessageDialog(null, Arrays.toString(arr));
gtiwari333
  • 24,554
  • 15
  • 75
  • 102