Woden
Woden

Reputation: 1376

Cannot continue program execution when throwing exception java

I am practicing using the try-catch block to validate user input. Trying so hard to do but don't know why it doesn't work. I tried it both ways. It always pops out InputMismatchException and end the program.

The first one, I followed this video: https://www.youtube.com/watch?v=PWez5mVXACc&t=356s

public int moveStone(int initialStone, int upperBound, int stoneBalance) throws Exception {
    int takeStone = 0;
    boolean isNumber = false;

    do {
        if (in.hasNextInt()){
            takeStone = in.nextInt();
            isNumber =true;
        } 
        if (!in.hasNextInt()) {
            if (stoneBalance >= upperBound) {
                System.out.println("Invalid move. You must remove between 1 and " + upperBound + " stones.\n");
                isNumber = false;
                takeStone = in.nextInt();
            }

            if (stoneBalance < upperBound) {
                System.out.println("Invalid move. You must remove between 1 and " + stoneBalance + " stones.\n");
                isNumber = false;
                takeStone = in.nextInt();
            }
        }
    } while (!(isNumber));
    return takeStone;
}   

and this, I followed by other tutorials:

public int moveStone(int initialStone, int upperBound, int stoneBalance) throws Exception {
    int takeStone = 0;
    try {
        if (in.hasNextLine()) {
            throw new Exception();
        } else {
            takeStone = in.nextInt();
            return takeStone;
        }

    } catch (Exception e) {
        System.out.println("Invalid move");
        if (stoneBalance >= upperBound) {
            System.out.println("You must remove between 1 and " + upperBound + " stones.\n");
            takeStone = in.nextInt();
            return takeStone;
        }

        if (stoneBalance < upperBound) {
            System.out.println("You must remove between 1 and " + stoneBalance + " stones.\n");
            takeStone = in.nextInt();
            return takeStone;
        }
    } 
    return -1;
}

Upvotes: 0

Views: 146

Answers (2)

Woden
Woden

Reputation: 1376

I got where the problem is: the user input still remains in the Scanner buffer.

  1. When it passes the invalid input, it goes to the catch block. This operation is correct.
  2. However, when the program has to prompt the user to give another input, the invalid input still in the Scanner that it goes straight to the takeStone = in.nextInt(). The program jumps out because of InputMismatchException directly.

The solution is to put another Scanner to take the token before prompting the user input.

public int moveStone(int initialStone, int upperBound, int stoneBalance) throws Exception {
    int takeStone = 0;
    try {
        if (!in.hasNextInt()) {
            throw new Exception();
        } else {
            takeStone = in.nextInt();
            return takeStone;
        }

    } catch (Exception e) {
        if (stoneBalance >= upperBound) {
            System.out.println("Invalid move. You must remove between 1 and " + upperBound + " stones.\n");
            in.next(); // input still in the Scanner buffer, use another scanner to take this input
            takeStone = in.nextInt();
            return takeStone;
        }

        if (stoneBalance < upperBound) {
            System.out.println("Invalid move. You must remove between 1 and " + stoneBalance + " stones.\n");
            in.next();
            takeStone = in.nextInt();
            return takeStone;
        }
    } 
    return -1;
}

Upvotes: 0

Vikas
Vikas

Reputation: 7165

You are throwing Exception

if (in.hasNextLine()) {
    throw new Exception();
}

And catching only InputMismatchException. Use Exception in catch

catch (Exception e){

Upvotes: 1

Related Questions