user918304
user918304

Reputation: 2165

How to determine the number of the input arguments in Java

Is there some methods to determine the number of input arguments from console in Java. Or just compare args[i] to null?

Upvotes: 2

Views: 4202

Answers (3)

Harry Joy
Harry Joy

Reputation: 59660

How about using args.length ?

Upvotes: 8

flash
flash

Reputation: 6810

You could use args.length to count the arguments. You could use a library like Apache Commons CLI to parse the arguments.

Upvotes: 1

michael667
michael667

Reputation: 3260

In public static void main(String[] args), args is an array. You can get the number of elements of an (any) array in Java with args.length. If there are no arguments, the array will have length 0, and args[0] will give you an ArrayIndexOutOfBoundsException.

Upvotes: 4

Related Questions