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);
}
}
}
new double[arrayLength]unless arrayLength is an integer.System.out.printlnafter it.