Error in the program

I wrote this program for a calculator:-
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
char ch;
do{
ch = scn.next().charAt(0);
int n1 = scn.nextInt();
int n2 = scn.nextInt();
int a;
if(ch==’+’){
a=n1+n2;
System.out.println(a);
}else if(ch==’-’){
a=n1-n2;
System.out.println(a);
}else if(ch==’’){
a=n1
n2;
System.out.println(a);
}else if(ch==’/’){
a=n1/n2;
System.out.println(a);
}else if(ch==’%’){
a=n1%n2;
System.out.println(a);
}else{
System.out.println(β€œInvalid Operation.Try again”);
}
}while(ch!=β€˜x’||ch!=β€˜X’);
}
}

And I got this error:-
Exception in thread β€œmain” java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:8)

see, these two lines
int n1 = scn.nextInt();
int n2 = scn.nextInt();
will work only when ch is one of arithmetic operator.
in your code even if ch is x, these two lines are executed and scanner didn’t get integers and throws exception.

Thanks.

please resolve it and rate it if satisfied.
Thanks

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.