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

2
  • 1
    Which version of JDK you are using ?? This is allowed only after Java7 Commented Sep 28, 2014 at 8:37
  • stackoverflow.com/questions/338206/… Commented Sep 28, 2014 at 8:38

2 Answers 2

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.");
        }
        }

    }

}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.