How to check for shortage of command line arguments
Hi, how do I check for a shortage of command line arguments entered, I tried this in main and it works, but is that proper coding technique?
Also, I want to know why it doesn's throw the ArrayIndexOutOfBoundsException to indicate too many command line arguments so how do I
define this, I am confused. Also, please see the commented out if-block, I want to make sure that when user enters
pos integers greater than the supported range for an int in Java, that it should give a user friendly error message but that
if-block doesn't run as I tried to compile it, how do I implement this then? (Note: I noticed that the NumberFormatException seems to catch values exceeding the range for pos integers, but I thought NumberFormatException is to check if in a given string there are non numeric characters...
Btw: the program for the sum method I written (not included) is supposed to sum the three command line arguments and return the int. (the sum method is for adding pos ints only I want)
Code :
public static void main(String[] args)
{
if ( args.length < 3 )
return;
/**HOW DO I CHECK FOR BOUNDARY VALUES THEN IF THE BELOW IF BLOCK DOESN'T WORK...****/
/if ( Integer.parseInt(args[0]) > Integer.MAX_VALUE |
// Integer.parseInt(args[1]) > Integer.MAX_VALUE |
// Integer.parseInt(args[2]) > Integer.MAX_VALUE )
// {
// System.out.println("Int inputs too large, not supported");
// return;
//}
try
{
sum(Integer.parseInt[0]);
sum(Integer.parseInt[1]);
sum(Integer.parseInt[2]);
}
catch (java.lang.ArrayIndexOutOfBoundsException exception)
{
System.out.println("T");
}
catch (NumberFormatException nfe)//to deal with non-int input
{
System.out.println("Inputs cannot be non-integers.");
}
}
Any help appreciated.
Re: How to check for shortage of command line arguments
Quote:
I tried this in main and it works, but is that proper coding technique?
Yes, it is.
Quote:
Also, I want to know why it doesn's throw the ArrayIndexOutOfBoundsException to indicate too many command line arguments so how do I
define this, I am confused.
Coz you return it if it has arguments less than 3. Try removing if(args.length<3) and pass two arguments to experience index out of bound exception.