javaexceptionerror-handlinginputmismatchexception

Java not asking for reinput after an InputMismatchException


I have a sample program which registers people for an airline.

At the Registration class, on the selectSeats method, I have a try catch block, where the catch statement should catch InputMismatchException in case the user inputs a non numeric value.

However, the reinput operation isn't happening, so instead, when an exception happens, the program just throws the error message and proceeds to the end (which leads to unexpected results)

This is the method in question

    public void seleccionarAsiento() {
            boolean malaSeleccion = true;
            do{
                try{

                    System.out.println("\n\n Digite el número de asiento del 0 hasta el 20");
                    while (malaSeleccion) {

                        //Selección del hashSet correspondiente a cada vuelo mediante uso de la variable polimorfica "asientos".
                        if(this.destino.equals("Nicaragua")) {
                            asientos = asientosNCA;
                        }
                        else if (this.destino.equals("Panama") || this.destino.equals("Panamá")) {
                            asientos = asientosPNA;
                        }

                        this.asiento = input.nextInt(); //The part causing the issue

                        if(this.asiento < 0 || this.asiento > 20) {
                            System.out.println("\nSelect a seat between 0 and 20.");

                        } else if (asientos.contains(this.asiento)) {
                            System.out.println("\nSeat taken, select another one.");
                        } else if (this.asiento >= 0 && this.asiento <= 20 && asientos.contains(this.asiento) == false) {
                            asientos.add(this.asiento);
                            continuarCiclo = false;
                            malaSeleccion = false;
                        }
                    }            

                } // Fin de bloque try

                //Bloque catch para prevenir un input no numerico.
                catch (InputMismatchException inputMismatchException) {
                    System.out.println("Not numeric value, try again.");
                    input.nextLine();

                }

In case this is relevant, since I'm not sure if this could be related to a problem with Inheritance (but I doubt it because the exception is being caught)

This is the start of the class where that method is, and an extension to Exception I added.

    public class RegistroCompra {

        Scanner input = new Scanner(System.in);
        private String destino;
        private int asiento;
        private boolean continuarCiclo = true;


        public static HashSet<Integer> asientosNCA = new HashSet(21);
        public static HashSet<Integer> asientosPNA = new HashSet(21);

        HashSet<Integer> asientos = null;

        class ExcepcionRegistro extends Exception {
            ExcepcionRegistro(String s) {
                super(s);
            }
        }

} while (continuarCiclo == true); // Fin de bloque Do

Edit: I solved the issue by making the method recursive in the catch block. So if it catches the inputmismatchexception (because it was catching it), it cleans the buffer from the invalid input with input.nextLine() and then, the function calls itself again to restart the selection process.


Solution

  • Do it as follows:

    public void selectSeat() {
        boolean valid = true;
        do {
            System.out.println("Enter the seat number from 0 to 20");
            // ...
            try {
                this.asient = Integer.parseInt(input.nextLine());
                // ...
            } catch (InputMismatchException inputMismatchException) {
                System.out.println("This is not a numerical value, try again.");
                valid = false;
            }
        } while (!valid);
    }