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: ");
}
}
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.
– user28595
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).
– Maniero