Reputation: 73
I have a simple question.How do i pass integer array as argument while running java -jar sample.jar.The integer array is my input.
There are some other options like passing the array as comma seperated values (like 1,2,3) and converting the string values as integer array in java.
I want to know if there is any other option. Thanks for the help!
Upvotes: 4
Views: 1974
Reputation: 3
It would be better to ask to the user to provide that parameters using something like this
For(int i<0; i<ARRAYSIZE; i++) {
Scanner reader = new Scanner(System.in);
System.out.println("Enter the number :");
array[i] = reader.nextInt();
}
Parameters are usually used to configure the program execution.
Upvotes: 0
Reputation: 533492
Command line arguments are text. You can pass them as a comma separated list which you can pass. Or you can pass a file name which contains the information saved in text or binary.
Upvotes: 4
Reputation: 1500175
No, there are no other options really. The command line arguments simply are passed as text. Some code, somewhere, has to parse them into integers.
There are various third party libraries available to parse the command line, but your entry point is certainly going to have a String[]
parameter. If all you need is an int[]
, I'd just convert each String
into an int
. In Java 8 this is really pretty simple:
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] array = Arrays.stream(args).mapToInt(Integer::parseInt).toArray();
// Just for example
for (int value : array) {
System.out.println(value);
}
}
}
Upvotes: 6