Results 1 to 7 of 7
  1. #1
    Calaminh is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Question Out of Bound Exception Error

    CodingBat Java Array-1 unlucky1

    My code:

    Java Code:
    public boolean unlucky1(int[] nums) {
    
    for(int i =0; i<nums.length; i++)
    {
        if (nums.length <= 1)
        {
            return false;
        }
        
     if((nums[0] == 1 && nums[1] == 3) || (nums[1] == 1 && nums[2] == 3))
     {
      return true;
     }
     
     if((nums[nums.length-1] == 1 && nums[nums.length] == 3) ||(nums[nums.length-2] == 1 && nums[nums.length-1] == 3))
     {
      return true;
     }
     
    }
    
    return false;
      
    }
    What do I do to fix this?

  2. #2
    HowardHyde is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Out of Bound Exception Error

    Array indexes in Java are zero-based. For example if your array has 4 elements in it, they are numbered 0, 1, 2, and 3, NOT 1, 2, 3, 4.
    The expression nums[nums.length] effectively attempts to refer to an element that is one greater than the highest-numbered element. Hence the ArrayIndexOutOfBoundsException. Use nums[nums.length-1] to get the last element in your array.

    Shameless plug: Moderator edit: removed
    Last edited by DarrylBurke; 01-27-2012 at 05:18 PM. Reason: Removed shameless plug

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,298
    Rep Power
    20

    Default Re: Out of Bound Exception Error

    Howard, there's a separate section of these forums for Reviews/Advertising. Please limit any shameless plugs to that area.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    HowardHyde is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: Out of Bound Exception Error

    Darryl,
    Thanks for pointing that out. I have posted there.
    The next time that I spend 2 minutes (or as occasionally happens, 2 hours) researching and answering someone's specific forum question, is it OK if I sign my reply 'Howard Hyde, author, Java Web Database Application Development at JavaWebDB.com'?

  5. #5
    Calaminh is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: Out of Bound Exception Error

    Quote Originally Posted by HowardHyde View Post
    Array indexes in Java are zero-based. For example if your array has 4 elements in it, they are numbered 0, 1, 2, and 3, NOT 1, 2, 3, 4.
    The expression nums[nums.length] effectively attempts to refer to an element that is one greater than the highest-numbered element. Hence the ArrayIndexOutOfBoundsException. Use nums[nums.length-1] to get the last element in your array.

    Shameless plug: Moderator edit: removed
    Um, I did start off the array using 0, and I did use nums[nums.length-1] ...?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default Re: Out of Bound Exception Error

    Quote Originally Posted by Calaminh View Post
    Um, I did start off the array using 0, and I did use nums[nums.length-1] ...?
    Ahem:

    Java Code:
    nums[nums.length] == 3
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Calaminh is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: Out of Bound Exception Error

    Quote Originally Posted by JosAH View Post
    Ahem:

    Java Code:
    nums[nums.length] == 3
    kind regards,

    Jos
    Oh, thanks for the catch.

Similar Threads

  1. Index out of bound exception
    By sh4rif in forum New To Java
    Replies: 7
    Last Post: 12-07-2011, 12:32 PM
  2. Array bound exception
    By lakshmibvaraprasad in forum New To Java
    Replies: 3
    Last Post: 07-19-2011, 02:02 PM
  3. array out of bound exception
    By farahm in forum New To Java
    Replies: 6
    Last Post: 12-19-2010, 09:10 PM
  4. Array index out of bound exception error
    By rahulkrishnanr in forum Threads and Synchronization
    Replies: 7
    Last Post: 10-12-2010, 05:57 PM
  5. ArrayIndexout of Bound exception
    By Preethi in forum New To Java
    Replies: 2
    Last Post: 02-14-2008, 09:40 PM

Posting Permissions

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