I am writing a program to determine gas mileage for a trip. However, I want to ensure the user does not enter a String or a Char at the prompt and ensure the user enters a Double. I set a try/catch, as I had in other programs with no issues, but this one is giving me issues. But the program keeps looping back through the method and then crashes. I have tried putting it in a while loop and tried putting each input in a separate try-catch with no luck.
private static double[] gas(){
double gasCost[] = new double[3];
System.out.println("********************************************************");
System.out.print("What is the current price of gas per gallon, ie...2.84: $");
try{
gasCost[0] = input.nextDouble();
System.out.print("On average, how many miles to the gallon does your vehicle get, ie...22.5: ");
gasCost[1] = input.nextDouble();
gasCost[2] = gasCost[0] / gasCost[1];
} catch (Exception e) {
System.out.println("\nERROR!! Invalid input, please try again!\n");
gas();
}
return gasCost;
}