Description
What I am aiming to do is request user to provide an input only to those entries which triggered an exception excluding those input requests which have been satisfactory
Example
Note: input is an instance of a Scanner
public Fluid addFluid() {
do{
try{
System.out.println("Please enter fluids ID: ");
f.setFluidID(input.next());
System.out.println("Please enter fluids molecular weight: ");
f.setMolecularWeight(input.nextDouble());
System.out.println("Please enter fluids temperature: ");
f.setTemperature(input.nextDouble());
error = false;
}
catch(InputMismatchException e){
System.out.println("Error! Please provide the right input.");
// if exception happens on input for setMolecularWeight(input.nextDouble()); skip print statement for fluids ID and f.setFluidID(input.next());
}
}
while(error != false);
getFluid().add(f);
System.out.println(getFluid());
return f;
}
Output
Please enter fluids ID:
propane
Please enter fluids molecular weight:
a
Error! Please provide the right input.
Please enter fluids ID: // how to exclude this print statement
Please enter fluids molecular weight:
Would there be a way how to do this using an if / else structure. I would want to avoid doing try / catch for every input operator.
Many thanks in advance.