I’ve been staring at this for hours and unable to think of a solution; I usually handle validation of this type with regex but am trying to use a built-in solution for a change (obviously, I don’t do this frequently):
private static double promptUserDecimal(){
Scanner scan = new Scanner(System.in);
System.out.println("Enter a decimal");
try{
double input2 = Double.parseDouble(scan.nextLine());
return input2;
} catch(NumberFormatException e){
System.out.println("Sorry, you provided an invalid option, please try again.");
}
}
The error with this is that the “return” isn’t found by the compiler so I get a compile error. If I put the “return” outside of the try/catch I need to declare/initialize “input2” which defeats the purpose of the operation. Any assistance is appreciated…
You need to return or throw something from (or after the catch). Judging by your output to the user, it looks like you just want to do the same thing again. Just call the method again and return the result.