2

So, the question is basic. In the code below, when I pass command line argument as java CommandLineDemo 3 5 * the file names in my current directory are being listed. Doing some research in Google I found out we should supply * as '*' in command line.

My question is, how can my code be modified so that it accepts '*' in command line and performs product of operand1 and operand2

class CommandLineDemo {
    public static void main(String[] args) {
        int operand1 = Integer.parseInt(args[0]);
        int operand2 = Integer.parseInt(args[1]);
        char theOperator = args[2].charAt(0);
        System.out.print(args[0] + args[2] + args[1] + " = ");
        switch(theOperator) {
            case ('+'):
                System.out.println(operand1 + operand2); break;
            case ('-'):
                System.out.println(operand1 - operand2); break;
            case ('*'):
                System.out.println(operand1 * operand2); break;
            case ('/'):
                System.out.println(operand1 / operand2); break;
            default:
                System.out.println("Invalid Operator selected");
        }
    }
}
Testaccount
  • 2,755
  • 3
  • 22
  • 27

3 Answers3

1

You can try passing the command line argument as a single string (Example: "2 3 +") with the below modified code.

import java.util.Arrays;

class CommandLineDemo {

    public static void main(String[] args) {

        String strArray = Arrays.toString(args);
        strArray = strArray.replace("[", "").replace("]", "").replaceAll("[, ]", "");
        String[] splits = strArray.split("");

        int operand1 = Integer.parseInt(splits[1]);
        int operand2 = Integer.parseInt(splits[2]);
        char theOperator = splits[3].charAt(0);

        System.out.print(splits[1] + " " + splits[3] + " " + splits[2] + " = ");

        switch(theOperator) {
            case ('+'):
                System.out.println(operand1 + operand2); break;
            case ('-'):
                System.out.println(operand1 - operand2); break;
            case ('*'):
                System.out.println(operand1 * operand2); break;
            case ('/'):
                System.out.println(operand1 / operand2); break;
            default:
                System.out.println("Invalid Operator selected");
        }
    }
}

The usage & output looks like this:

C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 +"
2 + 3 = 5

C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 -"
2 - 3 = -1

C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 *"
2 * 3 = 6

C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 /"
2 / 3 = 0

C:\Users\sarath_sivan\Desktop>java CommandLineDemo "2 3 a"
2 a 3 = Invalid Operator selected

C:\Users\sarath_sivan\Desktop>
1218985
  • 7,531
  • 2
  • 25
  • 31
0

* is a meta character in the shell, which means it has special meaning. So you need to escape it with the \ No need to modify the code , instead just type \* to mean *

codeMan
  • 5,730
  • 3
  • 27
  • 51
0

You don't need to extend you program. Passing * as '*' is because shell (or more precisely, Linux shell, have little knowledge about windows cmd :p) will perform file name expansion, if you pass * directly, it will be expanded as all the files in your current directory. '*' prevent this and pass * to your program as command line argument.

Summer_More_More_Tea
  • 12,740
  • 12
  • 51
  • 83