Java * command line arguments overloaded to get file names

I am writing the simplest calculator in java that picks up command line arguments. Everything works fine except for the multiplication function. For some reason, it picks up file names from its current location. I have absolutely no idea why this is happening and how to disable it ... google did not help.

public class Calculate {

    public static void main(String[] args) {

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

        if (args.length != 3) {
            System.out.println("<number1> <operator> <number2>");
        } else {

            final int num1 = Integer.parseInt(args[0]);
            final int num2 = Integer.parseInt(args[2]);
            switch (args[1]) {
            case "+":
                System.out.println(num1 + num2);
                break;
            case "-":
                System.out.println(num1 - num2);
                break;
            case "*":
                System.out.println(num1 * num2);
                break;
            case "/":
                System.out.println(num1 / num2);
                break;
            }
            System.out.println("\n");
        }
    }
}

enter image description here

+4
source share
2 answers

This has nothing to do with java, but with the command line you need to use:

java Calculate 2 "*" 3

, * : , , , . () . , , ('*') ("*") , intepret , .

, , , cmd :

java Calculate 2 <files in the directory> 3

, java , - , , .

( - Linux, ).

@PatriciaShanahan , , :

java Calculate "2 * 3"

, , args[0] "2 * 3" ( args 1, , ), . .

+5

:

java Calculate "2 * 3"

* .

+1

Source: https://habr.com/ru/post/1570089/


All Articles