command line - CommandLine Arguments in java Help -


i'm suppose use command-line arguments take user input , use enhanced loop sum.

this error:

exception in thread "main" java.lang.error: unresolved compilation problem: type mismatch: cannot convert double 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 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 %f ", sum);      }  }  } 

here how 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 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 %f ", sum);      }  } 

}


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

}


you summing array filled 0.0 (as default value), not command line arguments.

if want sum arguments, have iterate on them (=the args array), convert each argument double, , add these up. don't need double[] @ all.


edit: (after comments)

in example code of question using enhanced for-loop of array, seems know such loop is. so, use args array instead of myarray there. inside loop, integer.parseint(...) or double.parsedouble or similar, , add result sum variable.

if need more 1 statement in loop, use { ... } group them.


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -