import java.util.Scanner; import java.util.InputMismatchException; /** In this Item class, we'll have a default constructor that * asks the user to enter a name and a price. If there is an * input format error, we can handle the exception either here * or in the place where the constructor is called. * * It is usually better to handle a problem close to the source, * but this program is a demonstration of how you can "pass the buck." */ public class Item { private String name; private double price; /** Default constructor: ask user for input. */ public Item() // throws InputMismatchException { Scanner kbd = new Scanner(System.in); System.out.printf("Please enter a name and price: "); try { name = kbd.next(); price = kbd.nextDouble(); } catch (InputMismatchException e) { System.out.printf("Input error!\n"); System.exit(1); } } public String toString() { return name + " $ " + price; } }