user3530258
user3530258

Reputation: 13

Java : Accepting command line arguments for array of objects

I have an array like of objects in my main

Number[] NumberArray;

The class Item has this definition:

public class Number {
    int a1;
    int a2;
}

I need to accept input from command line like say 1 2 3 4 5 6, such that

Number[1].a1 = 2 

and

Number[1].a2 = 3 

and

Number[4].a1 = 5

and

Number[4].a2 = 6. 

How do I do this?

Please help.

Upvotes: 0

Views: 1215

Answers (1)

Anubian Noob
Anubian Noob

Reputation: 13596

In your main method:

public static void main(String[] args)

args is an array of command line parameters. To convert a String to an int, use Integer.valueOf().

For example, to get the integer value of the first commandline argument do the following:

Integer.parseInt(args[0]);

Note that this will crash if there are no arguments.

Upvotes: 1

Related Questions