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

17th Jun 2024, 9:10 AM
Danish Zubair
Danish Zubair - avatar
6 odpowiedzi
+ 1
Danish Zubair You can also try this input method int numerator = sc.nextInt(); int denominator = sc.nextInt(); String operator = sc.next();
17th Jun 2024, 2:05 PM
Buterbrod:)
Buterbrod:) - avatar
+ 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.
17th Jun 2024, 9:24 AM
Lisa
Lisa - avatar
+ 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
17th Jun 2024, 12:26 PM
Buterbrod:)
Buterbrod:) - avatar
+ 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
17th Jun 2024, 1:59 PM
Buterbrod:)
Buterbrod:) - avatar
+ 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.
17th Jun 2024, 3:25 PM
Danish Zubair
Danish Zubair - avatar
0
Buterbrod:) it still shows the same exception error
17th Jun 2024, 1:24 PM
Danish Zubair
Danish Zubair - avatar