2

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?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Vishal Khare
  • 73
  • 10
  • Can you write a method that takes an `int[]` and add up the numbers in it? – Code-Apprentice Jul 15 '14 at 23:37
  • This has been asked already. Please see [http://stackoverflow.com/questions/7574543/how-to-pass-console-arguments-to-application-in-eclipse][1] [1]: http://stackoverflow.com/questions/7574543/how-to-pass-console-arguments-to-application-in-eclipse – Kathir Jul 15 '14 at 23:39
  • The `eclipse` tag and "eclipse" in the title are typos for "ellipsis", aren't they? – David Conrad Jul 15 '14 at 23:52

3 Answers3

0
click on run menu > run configuration > arguments > program arguments

set parameters here

jmj
  • 237,923
  • 42
  • 401
  • 438
0

You don't need varargs to do this. Since the args parameter is already an array, let's think about this in terms of arrays. These are the steps you need to accomplish whta you want:

  1. Convert an array of String to an array of int.

  2. Add up the numbers in an array of int.

I suggest you try to figure out how to do each of these. If you run in to specific problems in doing so, please come back with more questions.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • I used the word ellipsis intentionally... those three dots are ellipsis and i think you people misunderstood it for eclipse. What you are saying won't help i think because i am using command prompt to execute my programs. i want to provide the arguments as command line argument. What does arrays have to do with it? – Vishal Khare Jul 15 '14 at 23:49
  • @VishalKhare You put "eclipse" in the title of your question. – David Conrad Jul 15 '14 at 23:52
  • @VishalKhare I had no such misunderstanding. The ellipsis you refer to is what programmers call `varargs`. My suggestion is to first learn about arrays before you worry about varargs because they are very close to the same thing. In fact, as you currently have it, the command-line arguments are declared as an array: `String[] args`. – Code-Apprentice Jul 15 '14 at 23:56
0

Click "Run" menu then click "Run configurations..." then click "arguments" tab. Just insert the 5 numbers here

enter image description here

After that just modify your code so you read all five inputs from args[]

In your code you can check how long the input is by using args.length which returns the length.

After that do a for loop like this:

    int totalSum = 0;
    for(int i = 0; i < args.length; i++){
       totalSum = totalSum + args[i];
    }
    System.out.printline(totalSum);

This will accept any length of input and sum them.

Jakkra
  • 641
  • 8
  • 25