2

I am trying to give command line argument to this program. But it gives Invalid operator when operator is * e.g. 2 3 *

import java.io.*;

import java.util.*;

public class Operation{

    public static void main(String args[]){
        int num1, num2, res=0;
        try{
            Exception ex = new Exception();
            if(args.length < 3)
                throw(ex);
        }catch (Exception e){
            System.out.println("Illegal no. of arguments");
            System.exit(0);
        }
        try{
            Exception ex1 = new Exception();
            num1 = Integer.parseInt(args[0]);
            num2 = Integer.parseInt(args[1]);
            if(args[2].equals("+")) 
                res = num1 + num2;
            else if(args[2].equals("-")) 
                res = num1 - num2;  
            else if(args[2].equals("*")) 
                res = num1 * num2;
            else if(args[2].equals("/")) 
                res = num1 / num2;
            else
                throw(ex1);
        }catch (Exception e){
            System.out.println("Invalid Operator");
            System.exit(0);
        } 
        try{
            Exception ex2 = new Exception();
            if(res < 0)
                throw(ex2);
            else
                System.out.println("Result = " + res);
        }catch (Exception e){
            System.out.println("Result is negative");
        }       
    }
} 
mkobit
  • 43,979
  • 12
  • 156
  • 150
Mukul Aggarwal
  • 1,515
  • 20
  • 16
  • * is a special character in command like. It give you the list of all files inside current folder – Vyncent Mar 23 '15 at 16:00
  • Some comments on coding style: there is no need to declare an exception before throwing it (unless you want to call additional methods on the created exception object). And personally, I suggest to ALWAYS use braces { } for if statements - no matter if there is just one statement. – GhostCat Mar 23 '15 at 16:02
  • 1
    I would say ALWAYS use braces { } for if/for/while/ ect... all statements :) – Vyncent Mar 23 '15 at 16:04
  • It's good practice to include the actual input given when reporting an "invalid input" message. So, something like `System.out.println("Invalid Operator: " + args[2]);` – Sizik Mar 23 '15 at 16:12

1 Answers1

3

Most likely, the * character will never arrive in your Java program. Typically, the shell that you use to invoke the java program will recognize it as wildcard and replace it with something; for example all files in the current directory. Have you tried "*" when invoking your program on the command line?

Alternatively, you could change your program: do not read the arguments from the command line; ask the user to type the values one by one, see Getting Keyboard Input

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I have tried "*" this already as shown above in the code. Suggest another way to execute multiplication using " * ". – Mukul Aggarwal Mar 23 '15 at 16:49
  • Sure, you are using "*" inside your Java code. But most likely, you simply typed `java Operation 2 3 *` on the command line. – GhostCat Mar 23 '15 at 16:52
  • can you provide me the correct code to multiply two numbers by taking input through command line. – Mukul Aggarwal Mar 23 '15 at 17:41
  • Can you try to use "*" when running your command? And why is it so important to do this via command line arguments? And why dont you just change the format of your arguments, for example you could "replace" * with a string like "multiply"; so "2 3 multiply"? – GhostCat Mar 24 '15 at 07:07