Java 在 InputMismatchException 之后不要求重新输入

Java not asking for reinput after an InputMismatchException

我有一个示例程序可以为一家航空公司注册人员。

在注册 class 的 selectSeats 方法中,我有一个 try catch 块,其中 catch 语句应该捕获 InputMismatchException,以防用户输入非数值。

然而,重新输入操作并没有发生,因此,当发生异常时,程序只是抛出错误消息并继续执行到最后(这会导致意外结果)

这是有问题的方法

    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

编辑:我通过在 catch 块中递归方法解决了这个问题。 因此,如果它捕获到输入不匹配异常(因为它正在捕获它),它会使用 input.nextLine() 清除缓冲区中的无效输入,然后该函数再次调用自身以重新启动选择过程。

该异常可能不是 InputMismatchException 的实例。您可以尝试添加异常 e 来查看真正的异常。


catch (InputMismatchException inputMismatchException) {
  System.out.println("Este no es un valor númerico, intente de nuevo.");
  input.nextLine();
}
catch (Exception e) {
  exception.printStackTrace()
  input.nextLine();
}

按如下操作:

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