4

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;
  • I don't think you can edit until you reach a certain rep points, so keep trying. Also, it would be nice if you isolated the part you want to ask and not posting the whole file. – nqngo May 12 '14 at 00:31
  • You should catch the exception in the try/catch block, then, as your program goals require, exit the try/catch block and try again either by looping through that block or by falling into a different try/catch. – Hot Licks May 12 '14 at 01:29

2 Answers2

3

It is because your try and catch block is run only once. You need to put it in a loop if you need to retry until success. Simply change your code block:

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

The try block will not reach break statement if there is an InputMismatchException is thorwn on nextDouble .

EDIT: Forgot to add but you also need to discard the old input so it won't raise the exception again. Hence the scanPrice.next() at the end. See this answer for more details: How to handle infinite loop caused by invalid input using Scanner

Community
  • 1
  • 1
nqngo
  • 528
  • 3
  • 10
  • thanks, an avast add-on didn't let me click edit. Your answer is the solution. My problem was trying to use a do while, but didn't know what condition to put into while. Could the while (true) be explained a bit? – user3626647 May 12 '14 at 00:43
  • While `true` simply means you run the loop for indefinitely until explicitly `break` out of the loop. – nqngo May 12 '14 at 00:46
3

Here, while(true) means that until your scanner is not getting the desired input(which is a double value in this case) it will keep asking you "Only numbers. Enter the cost again.". The moment the scanner gets the correct input then in that case no "InputMismatchException" will be thrown and the break statement in the try block will be executed, which takes your programs control to out of the while loop.

Amitesh Rai
  • 866
  • 11
  • 21