Boolean array Question

• 05-31-2011, 03:05 AM
elm101
Boolean array Question
Im relatively new to java and im having a problem with this program i'm working on.
Basically you enter a number n, which generates a boolean array for n number of doors.
Then that same number of n monkeys passes through opening and closing doors. If it's open it gets closed and if its closed it gets opened. The first monkey opens every door. The second monkey goes by and inspects every other door, the third monkey goes by and inspects every other third door, etc.
The problem is that when I run the program it gives me an Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 for line 10.

Here's my code:
Code:

```public static void main(String[] args) {         System.out.println("Enter the numbers of doors: ");         Scanner scan=new Scanner(System.in);         int dimension=scan.nextInt();         boolean numdoors[]=new boolean[dimension];         int numi=0;         int numj=0;         String result=new String();         for(int i=0; i<=numdoors.length; i++){           numdoors[i]=false;         }         for(int i=0; i<=numdoors.length; i++){           numi=i+1;           for(int j=0; i<=numdoors.length; j++){               numj=j+1;               if(numj%numi==0){                   if(numdoors[j]==false){                       numdoors[j]=true;                   } else if(numdoors[j]=true){                       numdoors[j]=false;                   }               }           }       }       for(int i=0; i<=numdoors.length; i++){           if(numdoors[i]==true){               result=result+" "+numdoors[i];           }       }       System.out.println(result);     }```
Any suggestions would be great!
• 05-31-2011, 03:39 AM
Fubarable
This looks like trouble:

Code:

`for(int j=0; i <= numdoors.length; j++){`
as j is going to eventually become numdoors.length, and when this happens and you try to call numdoors[j], your error will be thrown. This is why when doing a loop like this, we generally use < not <=.
• 05-31-2011, 03:41 AM
Norm
Remember the biggest index for an array of 10 items is 9.
• 05-31-2011, 05:53 AM
yellowledbet
Quote:

Originally Posted by Fubarable
This looks like trouble:

Code:

`for(int j=0; i <= numdoors.length; j++){`
as j is going to eventually become numdoors.length, and when this happens and you try to call numdoors[j], your error will be thrown. This is why when doing a loop like this, we generally use < not <=.

also the stopping condition of the inner loop is comparing the index of the outer loop. I would imagine that you want j to be i here " i <= numdoors.length"
• 05-31-2011, 07:58 AM
pbrockway2
Offtopic inasmuch as it adresses the problem rather than the code...

The monkeys are toggling the "state" of the doors of which they are a factor. So, once they're done the doors will record whether the corresponding number has an odd or even number of factors. Mostly numbers have an even number of factors (because they come in little/big pairs). A little thought should enable us to predict the exceptions (the ones with an "extra" factor) and hence the final state of all the doors - thereby saving the monkeys a lot of work.
• 05-31-2011, 09:44 PM
elm101
So I changed all the parameters of the for loops to < instead of <= and then I get the error in line
17.
• 05-31-2011, 10:36 PM
Norm
Please copy and paste here line 17.
Also post the FULL text of the error message. It answers questions like:
What is the value of the index?
• 05-31-2011, 10:51 PM
JosAH
Quote:

Originally Posted by pbrockway2
Offtopic inasmuch as it adresses the problem rather than the code...

The monkeys are toggling the "state" of the doors of which they are a factor. So, once they're done the doors will record whether the corresponding number has an odd or even number of factors. Mostly numbers have an even number of factors (because they come in little/big pairs). A little thought should enable us to predict the exceptions (the ones with an "extra" factor) and hence the final state of all the doors - thereby saving the monkeys a lot of work.

Can I just say that I don't understand the 'physics' of the problem given the original problem description? Your explanation makes me think of the sieve of Erastothenes but still I don't think I even understand what the problem is all about ...

kind regards,

Jos
• 06-01-2011, 06:03 AM
elm101
Jos:
the problem is from a programing contest from 2006. Here's the link:
/mars.wnec.edu/hscontest/ProblemSet2006.pdf
It's the first problem.

Norm: the problem occurs at this line:
Code:

`if(numdoors[j]==false){ //from the original code i posted`
here's the error I get:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
• 06-01-2011, 06:11 AM
Junky
You have already been told the answer. If your array has a length of 10 then the valid indicies are 0 to 9. You are allowing your loop to iterate too many times and j has a value 0f 10. Thus numdoors[j] is out of bounds.
• 06-01-2011, 08:02 AM
goldest
This is what it means,
Code:

```// Works Fine...         int[] x = { 1, 2, 3, 4, 5 };         for (int i = 0; i < x.length; i++) {                 System.out.print(x[i] + " ");         }         System.out.println(); // Throws ArrayIndexOutOfBoundsException         int[] y = { 1, 2, 3, 4, 5 };         for (int i = 0; i <[COLOR="red"]=[/COLOR] y.length; i++) {                 System.out.print(y[i] + " ");         }```
The red thing above cause it to happen. Check your code if it has something like that. Its actually pointed as well.

Goldest