Treat Inputmismatchexception calculator

Asked

Viewed 395 times

0

I am making a calculator in java oo, and I would like to treat this exception but I don’t 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: ");

    }
}
  • 1

    Inputmismatchexception occurs when the input of data does not match the expected type, type, in this code of yours, it is expected that if you enter integers, if you enter a character, throws the exception. Treat all entries as String and validate by parse if they are actually digits, this way you get around the exception.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

Do you really need a calculator class? Okay. There are several ways to treat the exception, I will present one of them, where he understands that something typed wrong should disregard and enter again with the data, no matter where the error was, but gives sophistication. 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 put in the Github for future reference.

I improved the code as a whole. I couldn’t test because I didn’t make a Minimum, Complete and Verifiable Example. I take this opportunity to say that I do not like how Java treats input wrong data. Particularly I 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’d have to write your own parser to avoid the exception.

  • He skips the other entrances, only treating one at a time.

  • I don’t know what that means.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.