switch statement's default is not working as expected.
when I enter other than number, control should execute default. But, it's not working as expected. And compiler throws error like Exception in thread "main" java.util.InputMismatchException
.
Kindly check.
import java.util.Scanner;
class Switch_Statement_2 {
public static void main(String[] args) {
// Add Two Entered Numbers
Scanner scan = new Scanner(System.in);
System.out.println("Enter First Number:");
int number1 = scan.nextInt();
System.out.println("Enter Second Number:");
int number2 = scan.nextInt();
System.out.println("Choose an Operator: + or *");
char operator = scan.next().charAt(0);
scan.close();
switch(operator) {
case '+':
number1 += number2;
System.out.println("Addition of Entered Numbers is: "+number1);
break;
case '*':
number1 *= number2;
System.out.println("Multiplication of Entered Numbers is: "+number1);
break;
default:
System.err.println("INVALID INPUT");
}
}
}