Results 1 to 11 of 11
  1. #1
    elm101 is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,424
    Rep Power
    25

    Default

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

  4. #4
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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. #6
    elm101 is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    So I changed all the parameters of the for loops to < instead of <= and then I get the error in line
    17.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,424
    Rep Power
    25

    Default

    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. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,528
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by pbrockway2 View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    elm101 is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    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. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    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. #11
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    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
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. boolean error help when no boolean is given
    By drewtrcy in forum New To Java
    Replies: 18
    Last Post: 05-05-2011, 09:04 AM
  2. Array question..
    By gerarda in forum New To Java
    Replies: 14
    Last Post: 03-02-2011, 01:42 AM
  3. Array question
    By TaxpayersMoney in forum New To Java
    Replies: 5
    Last Post: 06-11-2010, 01:41 AM
  4. Array Question
    By sc001 in forum New To Java
    Replies: 1
    Last Post: 02-14-2010, 04:57 AM
  5. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 02:18 AM

Posting Permissions

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