Note that there are some explanatory texts on larger screens.

plurals
  1. POPrinting arrays with different lengths
    primarykey
    data
    text
    <p>I'm writing a program that prints an array containing the sum of the values of two arrays passed as parameters. I need to include exceptions for when one of the arrays may be longer than the other. In that case, the method should print the sum of the indexes that both arrays share, then print the values of the array that don't have a corresponding value to add to. </p> <p>The problem here is that every time I go to compile the program, I get a message that says: Sum.java:51: cannot find symbol symbol : variable sumY location: class Sum sumY[j] = x[j] + y[j]; ^ 1 error</p> <pre><code>import java.util.*; class Sum { public static void main (String [] args) { double [] a1 = {4.5, 2.8, 3.4, 0.8}; double [] a2 = {1.4, 8.9, -1.0, 2.3}; arraySum (a1, a2); System.out.println (Arrays.toString (arraySum (a1, a2))); } public static double [] arraySum (double [] x, double [] y) { int length = 0; if (x.length &lt; y.length) { double [] sumY = new double [y.length]; length = y.length; for (int i = 0; i &lt;= y.length - 1; i++) { for (int j=0; j &lt;= x.length-1; j++) { sumY [j] = x[j] + y[j]; } sumY [(x.length -1) + i] = y[i]; } return sumY; } if (x.length &gt; y.length) { double [] sumX = new double [x.length]; length = x.length; for (int i = 0; i &lt;= x.length - 1; i++) { for (int j=0; j &lt;= y.length-1; j++) { sumY[j] = x[j] + y[j]; } sumX [(y.length -1) + i] = y[i]; } return sumX; } else { double [] sum = new double [x.length]; length = x.length; for (int i = 0; i &lt;= length - 1; i++) { sum[i] = x[i] + y[i]; } return sum; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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