Posts

Showing posts with the label Quartz

In Java, Converting Quartz To Gallons With Exception Handling

Image
import java.util.Scanner; public class QuartsToGallonsWithExceptionHandling{ public static void main(String[]args){ Scanner input = new Scanner(System.in); double quartz, gallons; do{ System.out.print("Enter Quartz: "); try{ if(!input.hasNextLine())break; quartz = new Double(input.nextLine()); if(0 > quartz){ System.out.println("Error: negative value!"); continue; } gallons = quartz * 0.25; System.out.printf("%n%.2f quartz is %.2f gallons%n", quartz, gallons); break; }catch(NumberFormatException e){ System.out.println("Error: Not a number!"); }catch(Exception e){ System.out.println("Error: Something went wrong!"); } }while(true); } }