0

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.

1
  • 2
    For starters, your requirement is super strange. Why are you skipping entries in the array? You also don't do anything with Number[0], which is the first entry in the array. To that end, when you write public static void main(String[] args), args would hold 1 2 3 4 5 6. Have you attempted anything to parse that array in your program? If you did, where are you stuck? Commented Apr 17, 2014 at 1:19

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

valueOf() returns Integer, which would then auto-unboxed to int, but cleaner to use parseInt()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.