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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started