I just learned two things- 1) How to use ellipsis in JAVA i.e. how to define a variable length argument list for a function. following is a program to demonstrate the above concept.
public class variable
{
public static void main(String[] args)
{
int d1=2;
int d2=3;
int d3=4;
int d4=5;
System.out.print(average(d1,d2,d3));
System.out.print(average(d1,d2));
System.out.print(average(d1,d2,d3,d4));
}
public static int average(int... numbers)
{
int total=0;
for(int i:numbers)
{
total+=i;
}
return total/numbers.length;
}
}
2) how to use command line argument. Following is a program that uses this concept-
public class argument
{
public static void main(String[] args)
{
if(args.length!=3)
{
System.out.println("Please provide valid 3 inputs to add them all");
}
else
{
int first = Integer.parseInt(args[0]);
int second = Integer.parseInt(args[1]);
int third = Integer.parseInt(args[2]);
System.out.println((first+second+third));
}
}
}
NOW... my question is how to use ellipsis in a program in which i want to give input via command line?
Suppose i want to add 3 numbers together through command line argument but my friend wants to add 5 numbers together. How do i use ellipsis in order to cater to both me and my friend's requirement?