In my code, I'm asking the user to input three different values divided by a blank space. Then, those three different values I would like to assign them to three different Double variables.
I have tried by assigning the first character of such string to my double variables but I haven't been able to succeed.
Any suggestions?
Thank you!
Here is what I'm trying to do:
int decision = message();
String newDimensions;
double newHeight, newWidth, newLength;
if(decision == 1){
newDimensions = JOptionPane.showInputDialog("Please enter the desired amount to be added" +
"\nto each dimension." +
"\nNOTE: First value is for Height, second for Width, third for Length" +
"\nAlso, input information has to have a blank space between each value." +
"\nEXAMPLE: 4 8 9");
newHeight = Double.parseDouble(newDimensions.charAt(0));
Try something like this:
There are still some defensive issues that can be taken. Doing a trim on your parse double is kind of critical.
You could first split the input using String.split and then parse each variable using Double.parseDouble Double.parseDouble to read them into a Double variable.
Get the input, split it by a whitespace and parse each Double. This code does not sanitize the input.
Take input from user.
split that line using delimeter space i.e " ".
inside for loop change each index element to double.By using Double.parseDouble(splitted[i]);
Double#parseDouble(str) expects a string,and you are trying to pass a character.
try this: