Write a program that accepts two numbers and a operator like (+,-,*, /) as command line arguments and perform the appropriate operation indicated by operator.If the user enters any other character the appropriate message will be displayed. The output of the program should be displayed to the user.
My Code is this
public class Practical4
{
public static void main(String[] args)
{
if(args.length==0)
{
System.out.println("No arguments are passed");
}
else
{
int a=Integer.parseInt(args[0]);
String p=args[1];
int b=Integer.parseInt(args[2]);
switch(p)
{
case "+":
System.out.println("Addition of "+a+" and "+b+" : "+(a+b));
break;
case "-":
System.out.println("Subtraction of "+a+" and "+b+" : "+(a-b));
break;
case "*":
System.out.println("Multiplication of "+a+" and "+b+" : "+(a*b));
break;
case "/":
System.out.println("Division of "+a+" and "+b+" : "+(a/b));
break;
case "%":
System.out.println("Modulo of "+a+" and "+b+" : "+(a%b));
break;
default:
System.out.println("Please Enter '+', '-', '*', '/' & '%' operator only.");
}
}
}
}
and i am getting this Error
java:17: incompatible types
found : java.lang.String
required: int
switch(p)
^
1 error
Please Give the Solution. Thanks