Results 1 to 3 of 3
  1. #1
    chronoz1300 is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default Array Index Out Of Bounds and Problem in Assigning Values

    Java Code:
    public class Ch6Exercise8 {
    
        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 why is it out of bounds...
    please help me here..


    what i want in this program is that i want to determine if a number is a prime number(im okay with this part),

    but im having problem with the ff:
    1.) store it in an integer[] array
    2.) the length of an array WILL ONLY DEPEND on the number of the values.
    ex: (ofcourse prime numbers are only divisible by 1 and itself) ,
    so i want to STORE those values in an array.
    but as the case have said, THE LENGTH OF THE ARRAY WILL ONLY
    DEPEND O N THE number (i.e Length) of the values. :confused:

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

    Default

    Check your loop for x == 2; no new array will be created and the first value is not equal to zero. I don't know what you want to do exactly but may I suggest you use a List<Integer> for all divisors instead?

    kind regards,

    Jos

  3. #3
    AndreB's Avatar
    AndreB is offline Senior Member
    Join Date
    Dec 2009
    Location
    Stuttgart, Germany
    Posts
    114
    Rep Power
    0

    Default

    Your first Exception is because of the inner loop:
    If your last number (where i == devisors.length-1) is not zero you jump to the else branch and try to access the array with [i+1] where i+1 is then greater than the bounds of an array

    it also very strage why you do that and maybe wrong.

    As for your assignment:
    Yes, you are right the borders depends on the given number but the number of devisors is limited: it is easy to see that the highest number of devisors would be n/2; namely the the maxmial amount of the smallest devisor: 2
    (of course this is not optimal solution)

    Also there are two approaches, depending on what you want to achieve.
    If you want to do a Primality test then the Sieve of Eratosthenes is a one easy way.
    If you want to do factorization look into integer factorization but there is no easy way do do it ;-)
    "There is no foolproof thing; fools are too smart."
    "Why can't you solve my Problem ?"

Similar Threads

  1. [SOLVED] Array index out of bounds exception
    By sruthi_2009 in forum New To Java
    Replies: 6
    Last Post: 03-23-2014, 10:16 AM
  2. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 07:42 AM
  3. New to Java, Arrays- index out of bounds
    By Connorhj in forum New To Java
    Replies: 8
    Last Post: 12-02-2009, 09:22 PM
  4. Array Index out of bounds
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 05:11 AM
  5. [SOLVED] Search problem - array out of bounds
    By viper110110 in forum New To Java
    Replies: 5
    Last Post: 11-26-2008, 04:26 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
  •