CommandLine Arguments in java Help

10,451

Solution 1

You are summing up an array filled with 0.0 (as this is the default value), not the command line arguments.

If you want to sum the arguments, you have to iterate over them (=the args array), convert each argument to a double, and add these up. You don't need the double[] at all.


Edit: (after some comments)

In your example code of your question you are using an enhanced for-loop of an array, so it seems you know what such a loop is. So, now use the args array instead of your myArray there. Inside the loop, you do the Integer.parseInt(...) or Double.parseDouble or similar, and add the result to your sum variable.

If you need more than one statement in the loop, use { ... } to group them.

Solution 2

arrayLength must be an integer. Thus

int arrayLength = Integer.parseInt(args[0]);
double [] myArray = new double [ arrayLength ];

Solution 3

It's also important to remember to think about the flow of your code:

You initialize an array. You sum the values in the array.

You never added values to the array, so you will always be summing a bunch of zeros.

Share:
10,451
Mugetsu
Author by

Mugetsu

Updated on June 04, 2022

Comments

  • Mugetsu
    Mugetsu almost 2 years

    I'm suppose to use Command-Line Arguments to take user input and than use an enhanced for loop to sum.

    This is the error:

    Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from double to int

    public class EnhanceForLoop {
    
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    
        if(args.length !=5)
            System.out.println(" please enter no more than 4 numbers");
        else
        {
    
        double sum; 
        double arrayLength = Double.parseDouble(args[0]);
        double [] myArray = new double [ arrayLength ];
    
        double value = Double.parseDouble((args[1]));
        double counter = Double.parseDouble((args[2]));
    
    
        for(double num: myArray)
          sum += num;
    
    
        System.out.printf("The sum is %f ", sum);
    
        }
    
    }
    
    }
    

    here is how it is so far:

    public class EnhanceForLoop {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    
        if(args.length !=5)
            System.out.println(" please enter no more than 4 numbers");
        else
        {
    
        double sum = 0.0; 
    
        int arrayLength = Integer.parseInt(args[0]);
        double [] myArray = new double [ arrayLength ];
    
        double num1 = Double.parseDouble((args[1]));
        double num2 = Double.parseDouble((args[2]));
        double num3 = Double.parseDouble((args[3]));
        double num4 = Double.parseDouble((args[4]));
        double num5 = Double.parseDouble((args[5]));
    
    
        for(double num: myArray)
          sum += num;
    
    
        System.out.printf("The sum is %f ", sum);
    
        }
    
    }
    

    }


    Here is the answer:

    public class EnhanceForLoop {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
    
        if(args.length !=5)
            System.out.println(" please enter no more than 4 numbers");
        else
        {
    
        double sum = 0.0; 
    
        int arrayLength = Integer.parseInt(args[0]);
        double [] myArray = new double [ arrayLength ];
    
        double num1 = Double.parseDouble((args[1]));
        double num2 = Double.parseDouble((args[2]));
        double num3 = Double.parseDouble((args[3]));
        double num4 = Double.parseDouble((args[4]));
    
    
    
        for(String s: args){
            sum += Double.parseDouble(s);
        }
          System.out.println("Sum: "+sum);
        }
    
    
    
    
    }
    

    }