, try catch . , . :
Scanner scanPrice = new Scanner(System.in);
System.out.println("Enter the cost: ");
try {
priceTag = scanPrice.nextDouble();
} catch (InputMismatchException e) {
System.out.println("Only numbers. Enter the cost again.");
scanPriceException = new Scanner(System.in);
priceTag = scanPriceException.nextDouble();
}
To:
Scanner scanPrice = new Scanner(System.in);
while (true) {
System.out.println("Enter the cost: ");
try {
priceTag = scanPrice.nextDouble();
break;
} catch (InputMismatchException e) {
System.out.println("Only numbers. Enter the cost again.");
scanPrice.next();
}
}
try break, nextDouble InputMismatchException.
: , , . , scanPrice.next() . . : ,