Parsing an array of comma-numbers obtained from an input dialog I made this nice bit of code that I can insert into any of my projects requiring user input. It seems quite long for what it does and I would love some incite as to how I can do this more efficiently i.e. fewer lines of code.

I love to make my code elegant, but I don't see how to do it any better.

    String x = (JOptionPane.showInputDialog(". Each patamater should be separated with a ',' and without spaces. (eg. 1,2,3)") + ",");
    int w = 0, a = 0, y = -1;
    for(int i = 0; i &lt; x.length(); i++)
    {
        if(x.charAt(i) == ',')
        {
            w++;
        }
    }
    Double[] z = new Double[w];
    for(int i = 0; i &lt; x.length(); i++)
    {
        if(x.charAt(i) == ',')
        {
            z[a] = Double.parseDouble(x.substring((y + 1), i));
            y = i;
            a++;
        }
    }
I suggest next way:

    String x = (JOptionPane.showInputDialog(". Each patamater should be separated with a ',' and without spaces. (eg. 1,2,3)") + ",");
    String[] split = x.split(",");
    Double[] z = new Double[split.length];
    int i = 0;
    for (String s : split) {
        if(s.trim().isEmpty()){
            continue;
        }
        try {
            z[i++] = Double.valueOf(s.trim());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    }


1)split() instead of loop for ','

2)validate spaces

read more about String class and it's functions.