I am new to Java and I want to solve a simple problem in java.
In input I need to take an integer a and then a character c and then another integer b
And print the output if the character is '+' then print a+b
And so on like this.
The input looks like : 6+4
But I find an error continuously like this
Exception in thread "main" java.util.InputMismatchException
My Code:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
try {
int a, b, ans = 0;
char c;
a = sc.nextInt();
c = sc1.next().charAt(0);
b = sc.nextInt();
if (c=='+') {
ans = a + b;
} else if (c=='-') {
ans = a - b;
} else if (c=='*') {
ans = a * b;
} else if (c=='/') {
ans = a / b;
}
System.out.println(ans);
} finally {
sc.close();
sc1.close();
}
}