Question: I need to Insert a try statement before prompting the user to Enter Miles Driven, then open a { and close } after the milesPerGallonCalculation:

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 ******************");

}

}

Step by Step Solution

3.32 Rating (149 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!