验证命令行参数的数量

时间:2021-07-23 23:19:26

I am trying to validate that there are only three arguments passed. In this program I am looking to pass two integers using command line arguments that look like 23 23 +. But I want an error to display if they enter less than three arguments or more than three arguments. As is right now, it gives the error message when there is more than three arguments but not less than three. Any help would be great please.

我试图验证只传递了三个参数。在这个程序中,我希望使用看起来像23 23 +的命令行参数传递两个整数。但是如果他们输入少于三个参数或多于三个参数,我想要显示错误。就像现在一样,当有三个以上的参数但不少于三个时,它会给出错误消息。任何帮助都会很棒。

public class Arithmetic {

public static void main(String[] args) {

    // setting up the variable firstNumber and secondNumber
    int firstNumber = Integer.parseInt(args[0]);
    int secondNumber = Integer.parseInt(args[1]);
    String arithmetic = args[2];


    int length = args.length;


        if(length != 3){
            System.out.println("Your suppose to enter an int, int then an operation sign like +,-,X or /.");
            return;
        }

     if (arithmetic.equals("+")) {
        int addition = firstNumber + secondNumber;
        System.out.println(args[0]+ " " + args[2] + " " + args[1] + " = " + addition);  
        int total = String.valueOf(addition).length();
        System.out.println(addition + " has the length of " + total);

    } else if (arithmetic.equals("-")) {
        int minus = firstNumber - secondNumber;
        System.out.println(args[0]+ " " + args[2] + " " + args[1]+ " = " + minus);
        int total = String.valueOf(minus).length();
        System.out.println(minus + " has the length of " + total);


    } else if (arithmetic.equals("/")) {
        int division = firstNumber / secondNumber;
        System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + division);
        int total = String.valueOf(division).length();
        System.out.println(division + " has the length of " + total);


    } else if (arithmetic.equals("x")) {
        int multiply = firstNumber * secondNumber;
        System.out.println(args[0] + " " + args[2] + " " + args[1] + " = " + multiply);         
        int total = String.valueOf(multiply).length();
        System.out.println(multiply + " has the length of " + total);


    }
    //following prints out to the console what the length of each argument is.
    System.out.println(args[0] + " has the length of " + args[0].length());
    System.out.println(args[1] + " has the length of " + args[1].length());
    System.out.println("The arguments that was passed would have been " + args[0]+ " " + args[1] + " " + args[2]);
}
}

1 个解决方案

#1


4  

You have code lines that assume that there are at least 3 arguments before you check how many arguments there are. Move these lines:

在检查有多少参数之前,您的代码行假定至少有3个参数。移动这些行:

// setting up the variable firstNumber and secondNumber
int firstNumber = Integer.parseInt(args[0]);
int secondNumber = Integer.parseInt(args[1]);
String arithmetic = args[2];

below your "!= 3" check. Otherwise if you enter less than 3 command line arguments, you'll get an ArrayIndexOutOfBoundsException on one of those lines before you check how many arguments you have.

低于你的“!= 3”支票。否则,如果输入少于3个命令行参数,则在检查有多少参数之前,将在其中一行上获得ArrayIndexOutOfBoundsException。

#1


4  

You have code lines that assume that there are at least 3 arguments before you check how many arguments there are. Move these lines:

在检查有多少参数之前,您的代码行假定至少有3个参数。移动这些行:

// setting up the variable firstNumber and secondNumber
int firstNumber = Integer.parseInt(args[0]);
int secondNumber = Integer.parseInt(args[1]);
String arithmetic = args[2];

below your "!= 3" check. Otherwise if you enter less than 3 command line arguments, you'll get an ArrayIndexOutOfBoundsException on one of those lines before you check how many arguments you have.

低于你的“!= 3”支票。否则,如果输入少于3个命令行参数,则在检查有多少参数之前,将在其中一行上获得ArrayIndexOutOfBoundsException。