简体   繁体   中英

Why is the return value of args.length 6 when I provide 3 command line arguments for my program

When I provide my Java Command line arguments with this: Calculate 3 * 3 and when I print args.length before making my operations, it returns 6 in the case of a multiplication.

Here is a screenshot of the results for java Calculate 3 * 3

在此处输入图像描述

Here is my code in two files:

  1. Calculator.java
  2. Calculate.java

Here is how to run it if Java is installed:

  • Open your terminal, run javac Calculate.java
  • When it compiles, run java Calculate 3 * 3

Note: Whenever you try a multiplication for this program, args.length is 6. The program expects one numbers as operands and an operator ("+", "-", "*", "/" for addition, subtraction, multiplication and division respectively). That program works fine for all operations except multiplication.

Calculator.java

import java.util.Map;
import java.util.HashMap;
import java.util.function.BinaryOperator;

public final class Calculator {
  // TODO: Fill this class in.
  private final Map<String, BinaryOperator<Integer>> operators = new HashMap<>();
  
  public void registerOperation(String symbol, BinaryOperator<Integer> operator){
  
    operators.put(symbol.strip(), operator);
  
  }
  
  public int calculate(int a, String operator, int b){
  
    return operators.get(operator).apply(a,b);
    
  }
 
}

Calculate.java

public final class Calculate {
  public static void main(String[] args) {
    System.out.println(args.length);
    System.out.println(args[2]);

    if (args.length != 3) {
      System.out.println("Usage: Calculate [int] [operator] [int]");
      return;
    }

    Calculator calculator = new Calculator();
    // TODO: Register the four "basic" binary operators: +, -, *, /
    calculator.registerOperation("+", (a, b) -> a + b );
    calculator.registerOperation("-", (a, b) -> a - b );
    calculator.registerOperation("*", (a, b) -> a * b );
    calculator.registerOperation("/", (a, b) -> a / b );

    int a = Integer.parseInt(args[0]);
    String operator = args[1];
    int b = Integer.parseInt(args[2]);

    System.out.println(calculator.calculate(a, operator, b));
  }
}

The * has a special meaning (all files in the current directory) to the command interpreter that starts java . This happens for all programs. Quote the * .

java Calculate 3 "*" 3

My advice would be to handle standard input (not arguments).

Here is one solution (on a Windows Computer)

I had managed to find a solution but I could not post it yet. Here is the trick, when you run java Calculate 5 * 7 for example in order to multiply 5 by 7, the * reacts unexpectedly, it returns all the files listed in the program's directory and in that case above it returns Calculate.class , Calculate.java , Calculator.class and Calculator.java . Hence running java Calculate 5 * 7 creates 6 arguments for our command: 5 (the first operand), Calculate.class , Calculate.java , Calculator.class , Calculator.java and 7 the second operand. To Tackle that I just added another condition on my code to detect when " ", * or ' ' is provided as an operator and it solved the issue. Here is the code:

Calculator.java

import java.util.Map;
import java.util.HashMap;
import java.util.function.BinaryOperator;

public final class Calculator {
    // TODO: Fill this class in.
    private final Map<String, BinaryOperator<Integer>> operators = new HashMap<>();

    public void registerOperation(String symbol, BinaryOperator<Integer> operator){

        operators.put(symbol.strip(), operator);

    }

    public int calculate(int a, String operator, int b){

        return operators.get(operator).apply(a,b);

    }

}

Calculate.java

import java.util.Arrays;

public final class Calculate {
    public static void main(String[] args) {
        // Because of * I get 6 arguments 
        // (the two operands + the 4 files generated when compiling
        // args[0] is the first operand
        // args[1], args[2], args[3] and args[4] are the files
        // args[5] is the second operand
        // The rest is just logic
 
        if(args.length == 6){
            args[1]  = "*";
            args[2] = args[5];
        }else {
            if (args.length != 3) {
                System.out.println("Usage: Calculate [int] [operator] [int]");
                return;
            }
        }



        Calculator calculator = new Calculator();
        // TODO: Register the four "basic" binary operators: +, -, *, /
        calculator.registerOperation("+", (a, b) -> a + b );
        calculator.registerOperation("-", (a, b) -> a - b );
        calculator.registerOperation("*", (a, b) -> a * b );
        calculator.registerOperation("/", (a, b) -> a / b );

        int a = Integer.parseInt(args[0]);
        String operator = args[1];
        int b = Integer.parseInt(args[2]);

        System.out.println(calculator.calculate(a, operator, b));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM