Developing a program that is similar to creating receipts. It takes scanner inputs: name and price. Trying to Use a try - catch for the situation where a double would not be inputted into the price scanner. Managed to get this to work, but for only if an exception is thrown once; and if I give incorrect input again inside the catch block it will fail. What could I do to have the program handle exceptions inside the catch? I'm also just a kid learning with whichever free resources I can get, so the error here might just be fundamental problems/bad coding practice and would like those to be pointed out as well.
Thank You!
Here is the code:
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();
}
costs[i] = priceTag;