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 4 of 4

Thread: Calculate primes help

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Calculate primes help

    I'm getting the following error. The idea is to have the user input a number, and then print out all the primes less than the number.

    Enter a value: 
    9
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    	at Primes.main(Primes.java:18)

    I've tried several numbers, most numbers don't give me the error, but 7, 9, 11 are a few that do. The ones that don't give me the error, what is wrong with my print statement that makes it so it never prints?

    Here is my code

    import java.util.*;
    public class Primes {
    	public static void main(String[] args){
    		Scanner stdin = new Scanner(System.in);
    		int x = 0;
    		boolean isPrime = true;
    		int ARRAY = 0;
    		int primes[] = new int[ARRAY];
     
    		System.out.println("Welcome to Prime Calculator!");
    		System.out.println("Enter a value: ");
    		x = stdin.nextInt();
     
    		for(int i = 2; i < (x/2) && isPrime == true; i++){
    			if (x % i == 0){
    				isPrime = false;
    			} else {
    				primes[ARRAY] = i;
    				ARRAY++;
    			}
    		}
    		for(int i = 0; i < ARRAY; i++){
    			System.out.println(primes[0]);
    		}
    	}
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Calculate primes help

    I'm not sure what algorithm you have there, but the most efficient method to do this is by the Sieve of Eratosthenes.

    You're getting the ArrayIndexOutOfBounds exception because you initialized primes[] to size 0, instead of to size x
    System.out.println("Welcome to Prime Calculator!");
    System.out.println("Enter a value: ");
    x = stdin.nextInt();
    primes = new int[x];

  3. #3
    Junior Member
    Join Date
    Sep 2009
    Posts
    11
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Calculate primes help

    I read a lot about that, and I feel like since we haven't learned about it, that's not what is expected in the program. My algorithm may not be the most efficient, but it seems like it should make sense? If the remainder of something divided by 2 = 0 then its not prime, if it is, try 3, if that is 0, then it's not prime, if it is, try 4, and so on..

    Substituting x into the array got rid of the error message, but now onlys prints the number 2--sometimes once, sometimes twice, sometimes three times

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Calculate primes help

    That's part of the fun of computer science: learning about new (well, in this case really old) algorithms that perform a given task.

    The reason why you're algorithm isn't working is because you're checking the primality of x. If you want to go about it that way, I'd recommend writing a helper function that will check to see if a number is prime, then everytime you find a prime number, print it out.

    for (int i = 2; i < x; i++)
    {
         if(isPrime(i))
         {
              System.out.println(i + " is prime");
         }
    }

    A simple primality checker:

    for every integer i from 2 to the number:
    if the number is divisible by i, return not prime
    end
    return true

    There are several optimizations that you can make, the biggest being that you only have to check numbers up to the square root of the number. The second is only to check to see if the number is divisible by 2, then return false. Then, only check to see if your number is divisible by odd numbers from 3 to sqrt(number)

Similar Threads

  1. Simple java program to calculate wall covering of a room
    By parvez07 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 22nd, 2009, 03:31 PM