I need to Insert a try statement before prompting the user to Enter Miles Driven, then open
Question:
I need to Insert a try statement before prompting the user to Enter Miles Driven, then open a { and close } after the milesPerGallonCalculation: 2) Insert a catch statement after the try } closing. 3) Add Finally at the very End of the program. 4) Run the project with the following Data: Miles Driven: Mary (You are entering a String instead of an Integer) The following message appears: Input String was Not in a correct format and the program is not interrupted.
package milespergallontrycatch;
import java.util.Scanner;
public class MilesPerGallonTryCatch {
/** * @param args the command line arguments */ public static void main(String[] args) { // 1) Create an object 'input' from Scanner class Scanner input = new Scanner (System.in); // 2) Print a Header for the project System.out.println(" **** Calculate Miles Per Gallon Project *** "); // 3) Declare the Local variables to be used in the main() method int milesDriven; int gallonsOfGasUsed; int milesPerGallon; // 4) Prompt user to input Miles Driven integer value, System.out.print("Please, Enter Miles Driven: "); milesDriven = input.nextInt(); // 5) Prompt user to input Gallons of Gas Used integer value, System.out.print("Please, Enter Gallons of Gas Used: "); gallonsOfGasUsed = input.nextInt(); // 6) Calculate Miles Per Gallon driven milesPerGallon = milesDriven / gallonsOfGasUsed; // 7) Print out the Miles Per Gallon result System.out.printf("The Car gets %d Miles per Gallon", milesPerGallon); // 8) Print a Footer for the Project System.out.println (" ************** End of Project ******************");
}
}