0

Easy code, but many errors 😰

import java.util.Scanner; public class calculator { public static void main(String[] args) { Scanner calc = new Scanner(System.in); System.out.println("Enter 1st number ____.(In order)"); double num1 = calc.nextInt(); System.out.println("Enter 2nd number ____.(In order)"); double num2 = calc.nextInt(); System.out.println("Enter the operation ____.(+ for addition, - for subtraction, * for multiplication and / for division)"); String operator = calc.nextLine(); switch(operator) { case "+": System.out.println("Sum of " + num1 + "and " + num2 + "is " + num1+num2); break; case "-": System.out.println("Difference of " + num1 + "and " + num2 + "is " + num1-num2); break; case "*": System.out.println("Product of " + num1 + "and " + num2 + "is " + num1*num2); break; case "/": System.out.println("Division of " + num1 + "and " + num2 + "is " + num1/num2); break; default: System.out.println("Noob i said you to type +/-/*// and you typed none Shame on you.") } } } ERROR: ./Playground/calculator.java:31: error: ';' expected System.out.println("Noob i said you to type +/-/*// and you typed none Shame on you.") ^ 1 error

8th Sep 2021, 12:37 PM
Ojas Kanotra
Ojas Kanotra - avatar
5 Réponses
+ 2
Doesn't the error message make it quite clear ? There is a missing semicolon at the end of last "system.out.println()" { inside "default" case }
8th Sep 2021, 12:41 PM
Arsenic
Arsenic - avatar
+ 1
Again, the error message says it all. You are trying to perform subtraction between a string and a double { When doing "is" + num1 - num2 } which is not possible. I think you are aiming for something like this { ..."is" + (num1 - num2) }
8th Sep 2021, 12:45 PM
Arsenic
Arsenic - avatar
+ 1
Ojas Kanotra 1 - If you don't use paranthesis () after string then that would be also string so add ( ) before and after each mathematical operation. 2 - nextInt() method returns int value so use nextDouble() for double value. 3 - semi-colon is necessary after each statement in Java. 4 - use next() method instead of nextLine() because nextLine() will read empty space also. https://code.sololearn.com/co6IC6yPDWbE/?ref=app
8th Sep 2021, 12:58 PM
A͢J
A͢J - avatar
0
Error code is : import java.util.Scanner; public class calculator { public static void main(String[] args) { Scanner calc = new Scanner(System.in); System.out.println("Enter 1st number ____.(In order)"); double num1 = calc.nextInt(); System.out.println("Enter 2nd number ____.(In order)"); double num2 = calc.nextInt(); System.out.println("Enter the operation ____.(+ for addition, - for subtraction, * for multiplication and / for division)"); ./Playground/calculator.java:19: error: bad operand types for binary operator '-' System.out.println("Difference of " + num1 + "and " + num2 + "is " + num1-num2); ^ first type: String second type: double 1 error
8th Sep 2021, 12:42 PM
Ojas Kanotra
Ojas Kanotra - avatar
0
Thanks. Main was calc.next() [As given by you] and calc.nextLine [What i was writing] and using perenthesis. It helped.
8th Sep 2021, 1:57 PM
Ojas Kanotra
Ojas Kanotra - avatar