Search code examples
javaarraysswingjoptionpane

Displaying Array with JOptionPane


First, this is a school activity, an activity that i cant solve by my own.

i'm a first year college BSIT student and this my first post.

i'm having trouble in displaying my "initialized array" in JOptionPane, i cant find the right code to display them. hope you get my question.

heres my code..

public static void main(String[] args) {

        String display="";

        String size = JOptionPane.showInputDialog("Enter Your Prefered Size Of Your Array");
        int newsize = Integer.parseInt(size);
        JOptionPane.showMessageDialog(null,"You Entered "+newsize+".");

        String array[] = new String[newsize];

        for (int a=0; a<array.length;a++)
        {
            JOptionPane.showInputDialog("Enter Value For Array["+a+"].");
        }

        for (int a=0;a<array.length;a++)
        {
            display = display +array[a];
        }

        JOptionPane.showMessageDialog(null,"\n"+array[display]);

    }

}

Solution

  •  JOptionPane.showInputDialog("Enter Value For Array["+a+"].");
    

    shoud be

    array[a]=JOptionPane.showInputDialog("Enter Value For Array["+a+"].");
    

    and then make string

    for (int a=0;a<array.length;a++)
        {
            display+=array[a]+","
        }
    

    after that just print it out :)

    for new line display+=array[a]+"\n"