3

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);
    }




}

}


2
  • You can't create an array with new double[arrayLength] unless arrayLength is an integer. Commented Feb 23, 2011 at 23:00
  • so, and now you can delete the superfluous code from your solution, about everything else than the loop and the System.out.println after it. Commented Feb 24, 2011 at 1:32

3 Answers 3

4

arrayLength must be an integer. Thus

int arrayLength = Integer.parseInt(args[0]);
double [] myArray = new double [ arrayLength ];
Sign up to request clarification or add additional context in comments.

2 Comments

In this line: double [] myArray = new double [ arrayLength ]; the constructor for an array takes only ints as argument. cast it to an int
Thanks, I fix that but unfortunately another problem appeared: when I compile the sum was 0.000000
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.

1 Comment

can you help me add with the enhance for loop
2

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.

5 Comments

[code]public static void main(String[] args) { int sum; int num1 = Integer.parseInt(args[0]); int num2 = Integer.parseInt(args[1]); sum = num1 + num2; System.out.printf("The sum is %d ", sum); } }[/code]
@Mugetsu: Don't add code in a comment (it is not readable here), add it to your question (if it is needed to understand it) - it has an edit link.
I'm supposed to use an enhance for loop to sum the numbers and I know how to do when I store the numbers like: `code' int sum; int num1 = Integer.parseInt(args[0]); int num2 = Integer.parseInt(args[1]); sum = num1 + num2; System.out.printf("The sum is %d ", sum);
how can i use the args array?
@Mugetsu: Put it at the place where you are using myArray now.

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.