Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Index Out Of Bounds

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Index Out Of Bounds

        public static void main(String[] args) {
     
            int[] divisors = new int[0];
     
            int number = 7,
                divisibilityCount = 0;
     
            for (int x = 1; x <= number; x++) {
     
                if (number % x == 0) {
     
                    divisibilityCount++;
                    divisors = new int[divisibilityCount];
                }
     
                for (int i = 0; i <= divisors.length - 1; i++) {
     
                    if (divisors[i] == 0) {
     
                        divisors[i] = x;
                    }
                    else {
     
                        divisors[i + 1] = x;
                    }
                }
            }
     
     
     
            for (int x = 0; x <= divisors.length - 1; x++) {
     
                System.out.println(divisors[x]);
            }
        }

    can figure it out....... help pls
    Last edited by chronoz13; December 28th, 2009 at 03:13 AM.


  2. #2
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Index Out Of Bounds

    Your 'for' continuation expression pretty much guarantees trying to access past the end of the array. Array indexes start at 0 and end at length - 1, so the continuation condition should normally be i < length. Initialising 'divisors' to a zero length array doesn't help - what's the point of that?

Similar Threads

  1. Having problems with String out of bounds errors
    By Bill_H in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 27th, 2009, 02:47 PM