Note that there are some explanatory texts on larger screens.

plurals
  1. POArrayIndexOutOfBoundsException message
    primarykey
    data
    text
    <p>I'm trying to write a program with a static method that accepts a single boolean array as its only parameter and then prints out the location of the first true, the last true, and the total number of trues in a string of consecutive true values. </p> <p>The program compiles fine. However, when I run it, only the first test is printed correctly. For the second test, I get an ArrayIndexOutOfBoundsException message. </p> <p>I've already read as many posts about it as I could find, but still haven't been able to make my program work properly. Any ideas as to what I'm doing wrong? Thanks so much!</p> <pre><code>class Bool { public static void main (String [] args) { boolean [] test1 = {false, true, true, true}; boolean [] test2 = {true}; boolean [] test3 = {true, true, true, true, false}; System.out.println ("For test 1: "); fullOfBool (test1); System.out.println ("For test 2: "); fullOfBool (test2); System.out.println ("For test 3: "); fullOfBool (test3); } public static void fullOfBool (boolean [] a) { int count = 0; int first = 0; int last = 0; for (int i = 0; i &lt;= a.length - 1; i++) { //this only finds the first true value in the array; either the value before was false or it's //already the first value in the array if (a[i] == true &amp;&amp; (a[i-1] != true || (i-1) &lt; 0) ) { first = i; System.out.println ("The first true occurs at position " + first + "."); break; //we can break because we've found the first true already } } for (int j = first; j &lt;= a.length - 1; j++) { if (a[j] == true) { count++; //(j+1) determines the last value; either (j+1) is false, in which case the a[j] is the last true, or //it is beyond the array's length, in which case a[j] is still the last true if ((((j+1) &lt; a.length) &amp;&amp; (a[j+1] != true)) || a.length == 1) { last = j; System.out.println ("The last true occurs at position " + j + "."); } } } System.out.println ("There was a sequence of " + count + " consecutive trues.\n"); } } </code></pre> <p>Thanks for all the input. You all were totally right, I overdid the first for-loop. Now I'm stuck on the second for-loop for j. It doesn't print anything for the first or second tests. I'm sure I've overdone this one as well, but can't figure out what needs to be fixed. </p> <pre><code> public static void fullOfBool (boolean [] a) { int count = 0; int first = 0; int last = 0; for (int i = 0; i &lt;= a.length - 1; i++) { //this only finds the first true value in the array; either the value before was false or it's //already the first value in the array if (a[i]) { first = i; System.out.println ("The first true occurs at position " + first + "."); break; //we can break because we've found the first true already } } for (int j = first; j &lt;= a.length - 1; j++) { if (a[j]) { count++; //(j+1) determines the last value; either (j+1) is false, in which case the a[j] is the last true, or //it is beyond the array's length, in which case a[j] is still the last true if ((((j+1) &lt; a.length) &amp;&amp; (a[j+1] != true))) { last = j; System.out.println ("The last true occurs at position " + j + "."); } } } </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