# Thread: Boolean array Question

1. Member
Join Date
May 2011
Posts
5
Rep Power
0

## 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:
Java 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!

2. This looks like trouble:

Java 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 <=.

3. Remember the biggest index for an array of 10 items is 9.

4. Senior Member
Join Date
Feb 2011
Location
Georgia, USA
Posts
122
Rep Power
0
Originally Posted by Fubarable
This looks like trouble:

Java 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"
Last edited by yellowledbet; 05-31-2011 at 04:59 AM.

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
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.

6. Member
Join Date
May 2011
Posts
5
Rep Power
0
So I changed all the parameters of the for loops to < instead of <= and then I get the error in line
17.

7. 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?

8. 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

9. Member
Join Date
May 2011
Posts
5
Rep Power
0
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:
Java 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

10. 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.

11. This is what it means,
Java 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•