Try/catch on switch creating infinite loop on run

Asked

Viewed 348 times

0

I’m also wanting to implement a try/catch within the while so that when a user enters a double with a dot instead of a comma, the program points the error and sends it back to code execution.

But when testing the error, the program enters a loop infinity, just putting a break the program stops running, but wanted to know if there is a possibility to return the execution of the program without having to run it again.

import java.util.InputMismatchException;
import java.util.Scanner;

public class Teste_Calc extends Calculadora {

    public static void main(String[] args)  {

        Calculadora c = new Calculadora();
        Scanner sc1 = new Scanner(System.in);
        double n1,n2;
        double resultado;
        int opcao;
        boolean x=true;

        while(x) {
            try {
            System.out.println("---------------------------------");
            System.out.println("Escolha uma das opções a seguir:");
            System.out.println("1) Somar!");
            System.out.println("2) Subtrair!");
            System.out.println("3) Multiplicar!");
            System.out.println("4) dividir!");
            System.out.println("5) Sair do programa!");
            System.out.println("---------------------------------");


            opcao = sc1.nextInt();


        if (opcao == 5) {
            System.out.println("Programa finalizado!");
            break;
        }

        else{

            switch(opcao) {


            case 1:

                System.out.println("Insita o valor do numero 1:");
            n1 = sc1.nextDouble();
                System.out.println("Insira o valor do numero 2:");
            n2 = sc1.nextDouble();
            resultado = c.somar(n1,n2);
            System.out.println("O resultado é: "+ resultado);

            break;

            case 2:
                System.out.println("Insita o valor do numero 1:");
                n1 = sc1.nextDouble();
                    System.out.println("Insira o valor do numero 2:");
                n2 = sc1.nextDouble();
                resultado = c.subtrair(n1,n2);
                System.out.println("O resultado é: "+ resultado);
                break;

            case 3:
                System.out.println("Insita o valor do numero 1:");
                n1 = sc1.nextDouble();
                    System.out.println("Insira o valor do numero 2:");
                n2 = sc1.nextDouble();
                resultado = c.Multiplicar(n1,n2);
                System.out.println("O resultado é: "+ resultado);
                break;

            case 4:
                System.out.println("Insita o valor do numero 1:");
                n1 = sc1.nextDouble();
                    System.out.println("Insira o valor do numero 2:");
                n2 = sc1.nextDouble();
                resultado = c.dividir(n1,n2);
                System.out.println("O resultado é: "+ resultado);
                break;  

                default:
                    System.out.println("Opção invalida");

            }

     }
            }

            catch(InputMismatchException exception) {
                System.out.println("Caracter inserido não compatível!" );
                 continue;
            }
        }       
    }
}
  • Try changing the value of x to false instead of setting break;

  • You are using the Try catch from the menu. You have to set the Try/catch to the code snippet that receives the input from your double.

  • 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

The code is confusing and so it is even difficult to find the error, and easy to make it. It’s too big a method with too much code repetition, but I’m not gonna try to fix it. At least keep things closer, keep the buildings smaller, don’t need to have a try so great. Actually I’m critical of the use of exception for this kind of thing, but then it’s a Java API error and you can do little (it might, but it’s too much work), so you have to deal with this problem. I took a few pieces to compile since I don’t have them, but this legacy seems very wrong too, even though it works. See how it gets simpler (I don’t even know where the bug was, I just organized and it worked):

import java.util.InputMismatchException;
import java.util.Scanner;

class Teste_Calc {
    public static void main(String[] args)  {
        while (true) {
            System.out.println("---------------------------------");
            System.out.println("Escolha uma das opções a seguir:");
            System.out.println("1) Somar!");
            System.out.println("2) Subtrair!");
            System.out.println("3) Multiplicar!");
            System.out.println("4) dividir!");
            System.out.println("5) Sair do programa!");
            System.out.println("---------------------------------");
            int opcao;
            try {
                Scanner sc1 = new Scanner(System.in);
                opcao = sc1.nextInt();
            } catch (InputMismatchException exception) {
                System.out.println("Caracter inserido não compatível!");
                continue;
            }
            if (opcao == 5) {
                System.out.println("Programa finalizado!");
                break;
            } else {
                switch (opcao) {
                case 1:
                    System.out.println("1) Somar!");
                    break;
                case 2:
                    System.out.println("2) Subtrair!");
                    break;
                case 3:
                    System.out.println("3) Multiplicar!");
                    break;
                case 4:
                    System.out.println("4) dividir!");
                    break;  
                default:
                    System.out.println("Opção invalida");
                }
            }
        }       
    }
}

I put in the Github for future reference.

Note how everything I do is closer to where it is used, including variable declaration.

Browser other questions tagged

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