-1
public class Eval {
    public static void main(String[] args) {

        int operand1;
        char exp1;
        int operand2;

        if (args.length != 3 ) {
            System.err.println("*** Program needs 3 arguements***");
            System.err.println("Usage: java Eval int1 exp int2");
            System.exit(1);
        }

        operand1 = Integer.parseInt(args[0]);

        exp1 = args[1].charAt(0);

        operand2 = Integer.parseInt(args[2]);


        System.out.print(args[0] + args[1] + args[2] + "=");

        switch(exp1){
            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(" Error.Invalid operator.");
        }
      }
    }

This is a program to do mathematical operations. Currently the program can do everything but multiply the integers, why does this happen? Furthermore, why is the (Usuage: java Eval) part necessary -- it doesn't work without it. Is there a way to avoid doing this? Lastly why are the args necessary. I'm sorry for so many questions, but I don't want to blindly write a program with no clear understanding of what I'm actually doing. Thank You very much for all the help and once again sorry for so many questions!

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724

1 Answers1

3

The problem with the multiplication is not in Java, but the command line itself. The * character is special on command lines, so escape it in the command line and you should be fine:

$ java Eval 6 * 9
*** Program needs 3 arguements***
Usage: java Eval int1 exp int2
$ java Eval 6 \* 9
6*9=54

The args are necessary here because Java takes the command line arguments and passes them to your program through the main parameter args.

You must say java Eval because you're really running the program java -- the JVM itself. Its first argument is the class containing the main method to run. All other command line arguments are passed to the args parameter of main.

The Java tutorial on the subject explains how command-line arguments work.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • It appears that when using Windows Command Prompt, using single quotes is the only thing that works (that I've found); `\*` does not, but rather is expanded to all files in the `\ ` root folder. The Java tutorial you linked at doesn't explain anything about wildcard expansion, by the way. On Un*x-type systems, this isn't a Java issue, of course, since the shell processes the `*` before Java ever sees it. P.S. I'm using the Java downloaded from Oracle, but other platforms may behave differently. – ajb Oct 07 '13 at 21:27
  • Thanks, also what would I need to do so if I add this statement if ( (args[1] == "/" ) && (args[2] == "0" ) ) { System.out.println("Cannot divide by zero"); } else { before the switch I only get the "cannot divide by zero and Exception in thread main – ExchangeChri Oct 07 '13 at 21:52
  • @user2827494 You can either test with `if` as you have it, or you can enclose the actual division in a try-catch block to catch the `ArithmeticException`. I would go with testing the arguments with `if` so you can detect the case yourself. But if you go with `if`, compare your `String` values with `String`'s `equals` method, not with `==`. – rgettman Oct 07 '13 at 21:58
  • @user2827494 If you go with `if`, please don't use string comparison to compare numbers. `args[2] == "0"` or (better) `args[2].equals("0")` will not catch it if you enter `"00"` as one of the arguments, but the program will still try to divide by zero. Instead wait until you call `parseInt` and then check `operand2 == 0`. – ajb Oct 07 '13 at 22:19