You can do this in a much simpler manner. Try creating a static method which takes some number and determines if it is prime

Code:

public static boolean isPrime(int n)

after you do that the problem is fairly trivial, all you will need to do from there is ask for 2 inputs and loop from the one to the other testing isPrime on each.

Also what errors are you getting?

What is interval supposed to do? It seems like it may be a waste.

Is the goal to find all primes from 1 - n? or from n1 - n2?

02-13-2011, 11:26 PM

hoosierfan24

interval is accepting to inputed numbers and all numbers in that range are imported into the array list.

and yes i am trying to find the primes from n1-n2

and im not getting any errors i am just not getting the correct prime numbers outputted

02-13-2011, 11:35 PM

sunde887

Code:

if ( divides( plist.get( m ), plist.get( n ) ) )

When the method reaches this statement, whats m? Whats n?
while filling an arraylist of all the possibles is a decent idea it's a bit overcomplicated.

Whats a prime number? How do you determine them? Also, are you getting compile time errors, or runtime exceptions? Post what they say.

Are you taking into account that when you remove an item from an arraylist all indexes are shifted down?

My biggest piece of advice for you is this, start small when you write your programs, make something fairly basic and move your way up until you reach your goal.

For this problem start by figuring out how to determine if a number is divisible by another, you got that part so you can not determine how to test if a number is prime, do that and post just that code.

02-13-2011, 11:44 PM

hoosierfan24

m is currently the first element in the arraylist and n is also the first element in the array list

if it is not prime though i also need to take it out of the arraylist. I need to print the the arraylist at the end that only contains the prime numbers in the interval

02-13-2011, 11:52 PM

sunde887

now create main, and pass some common prime numbers to your method to make sure it's working correctly(1, 3, 5, 7, 11), and pass some non prime numbers, the more you try the better you can test if you have successfully defined the method. From there you can now create(in main) the interval testing, or you can do it in a separate method.

Why did you choose to use a filled array list?
Are you taking into account that when you remove an item from an arraylist all indexes drop down one?