+ 1

Help me guys to solve this error shown in line no. 13

import java.util.Scanner; public class Calc { public static void main(String[] args) { System.out.println("Enter a no. the enter an operator then enyer a second no."); Scanner sc=new Scanner(System.in); int no_1=sc.nextInt(); char op=sc.next(); int no_2=sc.nextInt(); if (op == +) { System.out.println(no_1 + no_2); } } } // I want to make a simple code through which one can calculate numbers by adding subtracting dividing etc.

18th Mar 2017, 8:01 AM
Hemant Jaiswal
Hemant Jaiswal - avatar
4 Respostas
+ 2
Thanks Vishal, my code is working now and I will post the full version of this code together with the functions like subtraction division multiplication etc.
18th Mar 2017, 8:35 AM
Hemant Jaiswal
Hemant Jaiswal - avatar
+ 1
Since op is Char data type it must be enclosed with single quotation and use op.equals('+'). Alternately you can use op as data type string.
18th Mar 2017, 8:04 AM
Vishal Prajapati
+ 1
Thanks for your answer but after your sugesstion line no. 10 shows me an error in the code given below:- import java.util.Scanner; public class Calc { public static void main(String[] args) { System.out.println("Enter a no. the enter an operator then enyer a second no."); Scanner sc=new Scanner(System.in); int no_1=sc.nextInt(); char op=sc.next(); int no_2=sc.nextInt(); if (op == '+') { System.out.println(no_1 + no_2); } } } Help me how can I sovle this one
18th Mar 2017, 8:12 AM
Hemant Jaiswal
Hemant Jaiswal - avatar
+ 1
Change op variable type to String, @Hemant Jaiswal. Also in if condition change to op.equals("+") .
18th Mar 2017, 8:30 AM
Vishal Prajapati