Reputation: 189
So I can't get this little snipit of code to work and I'm not sure why it wont...
String rawInput = JOptionPane.showInputDialog("Please enter the three side lengths seperated by spaces.");
double[] hold = double.parseDouble(rawInput.split(" "));
I have an idea to use another string array to put the values in initially then put them into the double array using a for loop and the double.parseDouble() but that seems overly complicated for what I want to do.
Upvotes: 0
Views: 31
Reputation: 4346
String rawInput = JOptionPane.showInputDialog("Please enter the three side lengths seperated by spaces.");
String[] inputs = rawInput.split(" ");
double[] values = new double[inputs.length];
for (int i = 0; i < inputs.length; i++) {
values[i] = Double.parseDouble(inputs[i]);
}
Try this!
Upvotes: 0
Reputation: 189
What I ended up doing was this:
String rawInput = JOptionPane.showInputDialog("Please enter the three side lengths seperated by spaces.");
double[] doubleHold = new double[3];
String[] stringHold = rawInput.split(" ");
for(int i = 0; i < 3; i++)
{
doubleHold[i] = Double.parseDouble(stringHold[i]);
}
I don't like how it works and think there must be a better way, anyone have that better way?
Upvotes: 0
Reputation: 79807
Your code fails to compile because rawInput.split(" ")
returns an array of String objects. You need just one String
object to pass to Double.parseDouble()
. It looks like you'll need a loop, to iterate through the array that you get.
Upvotes: 2