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 < x.length(); i++)
{
if(x.charAt(i) == ',')
{
w++;
}
}
Double[] z = new Double[w];
for(int i = 0; i < x.length(); i++)
{
if(x.charAt(i) == ',')
{
z[a] = Double.parseDouble(x.substring((y + 1), i));
y = i;
a++;
}
}
orderEveryDogInTheWorld();
will take more time than callingint x = 0;
andx = x + 1;
, even though the second example is two lines. – sdasdadas Nov 8 '13 at 19:12