Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions

Question

How is Tata Group addressing those challenges?

Answered: 1 week ago

Question

600 lb 20 0.5 ft 30 30 5 ft

Answered: 1 week ago