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

}

arrayLength needs to be an int.

arrayLength needs to be an int.

thanks. I fixed that but when I compiled it the answer was 0.000000? is there a way i can fix this?

I'm not sure why you are setting up all that other stuff with the values from args. It sounds like all you need to do is loop over the args, convert them to numbers and add them up.

I can do this:

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



    }

}

but i don't know how I can add them with a enhanced for loop.

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

}

if i may add in, can't u just do this?

public static void main(String[] args){
        int sum=0;
        for(String s: args){
            sum += Integer.parseInt(s);
        }
        System.out.println("Sum: "+sum);
    }

do you mean like this:
if yes i got errors

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]));
		
		//sum = num1+num2+num3+num4; 
		
		
		   
		    double sum=0.0;
		   
		      for(String s: args){
		  
		      sum += Integer.parseInt(s);
		  
		      }
		  
		      System.out.println("Sum: "+sum);
		
		      }
		
		
		/*for(double num: myArray)
		  sum += num++;

		
		System.out.printf("The sum is %f ", sum);*/
		
		

	}

}

Thanks :). I figured it out:

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

	}

}

Thanks everyone for responding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.