Question: Having trouble writting my getInput() method In java. I know it has to be something simple I'm not seeing, thanks in advance: Exception-al In this

Having trouble writting my getInput() method In java. I know it has to be something simple I'm not seeing, thanks in advance:

Exception-al

In this lab, we'll play around with the concepts related to exceptions that were discussed in the lecture. The goals are:

Create a custom exception class

Throw an exception from deep within a call stack

Catch it in main

Handle the exception

Perform the same action whether an exception is thrown or not (finally).

Steps

Create a new Class that "is an" Exception

Call the base (super) constructor with the same message, but append " - my exception" to the text

Create a main function in a driver classInside a loop, call a function to prompt the user to input miles driven and gallons used

If either number is negative, do not add the values to the totals

If something other than an integer is entered, assume the user intended to display the output and exit the program.

After each pass of the loop, display "------------", even after the user has exited by typing in a string

Display miles per gallon calculation

This should be a separate function

Don't forget to consider the case when the very first value the user enters is a string

My code:

import java.util.*;

public class Driver

{

//Display miles per gallon calculation

public int displayGallonsCalculation(int gallons)

{

return gallons;

}

public int getInput(Scanner cin) throws IsTheException

{

int milesDriven;

int gallons;

System.out.print("Enter miles driven on this tank: ");

milesDriven = cin.nextInt();

System.out.print(" ");

System.out.print("Enter number of gallons: ");

gallons = cin.nextInt();

System.out.print(" ");

return getInput(milesDriven, gallons);

}

public static void main(String[] args)

{

Driver d = new Driver();

Scanner cin = new Scanner(System.in);

int userInputs;

//Inside a loop, call a function to prompt the user to input miles driven and gallons used

while(true)

{

try

{

userInputs = d.getInput();

}catch(Exception e)

{

System.out.println(e.getMessage());

}

finally

{

System.out.println("----");

}

}

}

}

Step by Step Solution

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 Databases Questions!