Search code examples
javaarraysjoptionpane

Arrays and JOptionPaneOption


I cant find a way to convert this to int.

here's the code

int array[] = new int [newsize];

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

how to convert this into int..(sorry bad question)

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

thank you for helping for parsing first the inputted string to int

now ihave new problem. how can display ZERO, POSITIVE, NEGATIVE string with corresponding array in one JOptionPnae message..

heres my whole new code....

String display=""; int z = 0;

    String size = JOptionPane.showInputDialog("Enter Your Prefered Size Of Your Array");
    int newsize = Integer.parseInt(size);

    JOptionPane.showMessageDialog(null,"You Entered "+newsize+".");

    int array[] = new int [newsize];

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

    }



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

        if (z == array[a])
        {
          String c=array[a]+"  ZERO";
          JOptionPane.showMessageDialog(null,"Arrays\n"+display+c);
        }
        else if (z < array[a])
        {
            String c =array[a]+"  POSITIVE";
            JOptionPane.showMessageDialog(null,"Arrays\n"+display+c);
        }
        else if (z != array[a])
        {
            String c =array[a]+"  NEGATIVE";
            JOptionPane.showMessageDialog(null,"Arrays\n"+display+c);
        }

        JOptionPane.showMessageDialog(null,"Arrays\n"+display+c);
     }

hope you get my question..


Solution

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

    You should also do a check that the input value is an int, otherwise an exception will be thrown.