In Java, Converting Quartz To Gallons With Exception Handling

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);
	}
}

Comments

Popular posts from this blog