Question: You've created an application named quartstogallonsinteractive that accepts a number of quarts from a user and converts the value to gallons. Now, add an exception-handling

You've created an application named quartstogallonsinteractive that accepts a number of quarts from a user and converts the value to gallons. Now, add an exception-handling capabilities to this program and continuously reprompt the user while any nonnumeric value is entered. Save the file as QuartsToGallonsWithExceptionHandling.java

What I have will compile with no errors but doesn't do anything to prompt user for values.

import java.util.Scanner; class QuartsToGallonsInteractiveExceptionHandling { public static void main(String[] args) { // Set your variables Scanner input = new Scanner(System.in); final int oneQuart = 4; int quarts; boolean isGoodUserEntry = true; while(!isGoodUserEntry) { System.out.println("Enter number of quarts"); try{ quarts = Integer.parseInt(input.nextLine()); int gallons = quarts / oneQuart; quarts = quarts % oneQuart; System.out.println("A job that needs " + gallons + " gallons plus " + quarts + " quarts."); isGoodUserEntry = false; } // Catch the exception if it's not good catch(Exception e){ System.out.println("Exception: " + " NumberFormatException"); } } } }

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!