+ 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.
4 Respuestas
+ 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.
+ 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.
+ 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
+ 1
Change op variable type to String, @Hemant Jaiswal. Also in if condition change to op.equals("+") .



