1

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

Rustam
  • 6,485
  • 1
  • 25
  • 25
Prince
  • 33
  • 2
  • 2
  • 8

2 Answers2

1

try this: because java 7 onward string is supported in switch case.

before java 7 you should do like 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]);
        char p=args[1].charAt(0);
        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.");
        }
        }

    }

}
Rustam
  • 6,485
  • 1
  • 25
  • 25
0

In old versions of java, you cannot iterate in switch over a string. That is allowed only since java 7: https://stackoverflow.com/a/12521398/1276062 Either update your java version or convert the p to char

Community
  • 1
  • 1
Kyborek
  • 1,519
  • 11
  • 20