Question

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.

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]);
}
}
Was it helpful?

Solution

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

// 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top