0
Input Mismatch Error in Java
Hi, I was creating a calculator program, which take the numerator, denominator and the operator as the input and should display the correct output, but I amhaving an Input Mismatch Error. I have searched online but nothing solved the issue. Would anyone be able to solve the issue? Here is the code bit: https://sololearn.com/compiler-playground/c96n8tjgILRo/?ref=app
6 Antworten
+ 1
Danish Zubair
You can also try this input method
int numerator = sc.nextInt();
int denominator = sc.nextInt();
String operator = sc.next();
+ 4
You first have to enter the numbers, then the operator – that's how you defined it in your code.
Also, find out about how to compare strings in Java. Your switch statement won't work that way.
+ 2
Try this
int numerator = sc.nextInt();
int denominator = sc.nextInt();
sc.nextLine();
String operator = sc.nextLine();
The point is that the "nextInt" method scans the string up to the number, i.e. you can enter 2 numbers in one line, and each method will recognize it, but will not go to the next line
+ 1
Danish Zubair
Strange, everything works for me
What input did you make?
I made the following input
(This is if you added that line of code that I mentioned above)
number
number
operator
+ 1
Buterbrod:) yeah found the solution, I converted from String type to character type the variable 'operator' and wrote 'sc.next()' and it worked. Thank you so much for helping me.
0
Buterbrod:) it still shows the same exception error