Note that there are some explanatory texts on larger screens.

plurals
  1. POLoops and arrays not following through
    primarykey
    data
    text
    <pre><code>int [] f = {1,2,3,4,5,5,4,3,2,1}; int [] b = {6,1}; System.out.println(Arrays.toString(hide(f,b))); public static int [] hide(int [] front, int [] back) { int temp; int extraTemp; int nextTemp = 0; int [] hiddenAt = new int[front.length]; //int [] shownAt = new int[front.length]; for(int x = 0; x &lt; front.length; x++){ for(int y = 0; y &lt;= back.length; y++){ temp = x; if ((back.length &gt; front.length) || (front[x] &lt; 0 || back[y] &lt; 0) || (front.length &lt; 1 || back.length &lt; 1)) { System.exit(0); } if (y &lt; back.length - 1){ nextTemp = Math.abs(back[y + 1] - front[x + 1]); } else { nextTemp = 0; } if (front[x] &gt; back[y]) { System.out.println(temp); } else if (front[x] &lt; back[y] &amp;&amp; y &gt;= back.length - 1 ) { extraTemp = back[y] - front[x]; if (extraTemp &gt; nextTemp){ extraTemp = nextTemp; } System.out.println(extraTemp); } else if (front[x] &lt; back[y]) { extraTemp = back[y] - front[x]; if (extraTemp &gt; nextTemp){ extraTemp = nextTemp; } System.out.println(extraTemp); } } } return hiddenAt; } </code></pre> <p>The <code>println</code>'s are being substituted in to see the values that are being produced. They will be <code>hiddenAt[z] = temp;</code> when I figure out the values to be correct.</p> <p>I need it to compare <code>front[0]</code> with <code>back[0]</code> then <code>front[1]</code> with <code>back[1]</code>, etc. After that, it will shift over one: <code>front[1]</code> with <code>back[1]</code> then <code>front[2]</code> with <code>back[2]</code> until the end of <code>front[]</code>. I want to find out where the lowest difference is. (In this case when the number in the []'s is 4 and 5.)</p> <p>I get two errors, one saying line 39 if ((back.length > front.length) || (front[x] &lt; 0 || back[y] &lt; 0) || (front.length &lt; 1 || back.length &lt; 1)) { System.exit(0); }</p> <p>is wrong and that line 9 is wrong as well. public class 2 { public static void main(String[] args) {</p> <pre><code> int [] f = {1,2,3,4,5,5,4,3,2,1}; int [] b = {6,1}; System.out.println(Arrays.toString(hide(f,b))); } </code></pre> <p>the exact error is:</p> <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at hw2.hide(hw2.java:39) at hw2.main(hw2.java:9) </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