Search code examples
javaarraysprintingjoptionpane

Printing an array in JOptionPane


public String show(int[] row)
    {
        for(int i = 0; i < row.length; i++)
        {
            JOptionPane.showMessageDialog(null, row[i]);
        }
    }

What is wrong with this code? I just want to print an array which is given in the parameter.


Solution

  • You could change the return type to void:

    public void show(int[] row) {
    

    Also, if you wish to display all elements in the array you could do:

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

    If a return String is needed the same approach can be used:

    return Arrays.toString(row);