Note that there are some explanatory texts on larger screens.

plurals
  1. POCommandLine Arguments in java Help
    text
    copied!<p>I'm suppose to use Command-Line Arguments to take user input and than use an enhanced for loop to sum.</p> <p>This is the error:</p> <p>Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from double to int</p> <pre><code>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); } } } </code></pre> <p>here is how it is so far:</p> <p>public class EnhanceForLoop {</p> <pre><code>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); } } </code></pre> <p>}</p> <hr> <p>Here is the answer:</p> <p>public class EnhanceForLoop {</p> <pre><code>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); } } </code></pre> <p>}</p> <hr>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload