Treat InputMismatchException Calculator

0

I'm doing a calculator in java oo, and would like to handle this exception but I do not know how.

This is the main class.

public class Principal {

    public static void main(String[] args) {


        Calculadora calc = null;

        int opc = -1, num1, num2;
        Scanner sc = new Scanner(System.in);

        do {

            System.out.println("Entre com o primeiro numero: ");
            num1 = sc.nextInt();

            System.out.println("Entre com o segundo numero: ");
            num2 = sc.nextInt();

            menu();
            opc = sc.nextInt();


switch (opc) {

        case 0:
            break;

        case 1:

             calc = new Calculadora(new Soma(), num1, num2);
             System.out.println("\nResultado: " + calc);

            break;

        case 2:

            calc = new Calculadora(new Subtracao(), num1, num2);
            System.out.println("Resultado: " + calc + "\n");

            break;

        default:
            System.out.println("Opcao Invalida.");

            break;
    }


        } while(opc != 0);
    }

    static void menu() {

        System.out.println("\tEscolha uma Opcao ");
        System.out.println("0: Sair");
        System.out.println("1: Somar");
        System.out.println("2: Subtracao");
        System.out.print("Opcao: ");

    }
}
    
asked by anonymous 17.11.2016 / 13:12

1 answer

1

Do you really need a calculator class? Okay. There are several ways to handle the exception, I'll introduce one of them, where he understands that something typed wrong should disregard and re-enter the data, no matter where the error was, but it can be sophisticated. I did so:

public class Principal {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        do {
            try {
                System.out.println("Entre com o primeiro numero: ");
                int num1 = sc.nextInt();
                System.out.println("Entre com o segundo numero: ");
                int num2 = sc.nextInt();
                menu(); //completa desnecessidade fazer isso
                int opc = sc.nextInt();
            } catch (InputMismatchException) {
                System.out.println("Você digitou algo irregular, vamos começar de novo");
                continue;
            }
            switch (opc) {
                case 0:
                    break;
                case 1:
                     System.out.println("\nResultado: " + new Calculadora(new Soma(), num1, num2));
                    break;
                case 2:
                    System.out.println("Resultado: " + new Calculadora(new Subtracao(), num1, num2) + "\n");
                    break;
                default:
                    System.out.println("Opcao Invalida.");
                    break;
            }
        } while (opc != 0);
    }

    static void menu() {
        System.out.println("\tEscolha uma Opcao ");
        System.out.println("0: Sair");
        System.out.println("1: Somar");
        System.out.println("2: Subtracao");
        System.out.print("Opcao: ");
    }
}

I've improved the code as a whole. I was unable to test because a Minimum, Full, and Verifiable example was not done. I take it to say that I do not like how Java handles wrong data entry. I particularly prefer to do without the use of exception and treat the error individually by the normal flow. It gives more work, but it gets better. But this is not the way Java works, although it can be done normally.

To do it right, in practice you would have to write your own parser routine to avoid the exception.

    
17.11.2016 / 13:27