Why have you got that for loop with no continuation condition? Why have you nested it inside a while loop? Why do you have a constructor allowing you to set the size of your array and then test it against 100? You have a serious design flaw in the get(int) method; what happens if you have a 50-element array and look for no 60?

How are you calculating prime numbers? You ought to write it down on paper and get an algorithm working. Those three nested loops look very complicated to me, and suggest to me there is a better way to so that.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 42561

31

posted

0

alper cem polat wrote: . . . The Intbag class . . . Through that class, i will calculate the first 100 prime numbers. . . .

No, you aren’t. You are using something else to try and calculate prime numbers.

You don’t think a Sieve of Eratosthenes would be better?

Have a closer look at your prime variable. You initialise it to true, it gets set to false the first time you find a prime factor...and it never gets changed again.

Your combination of a while and for loop on lines 19 and 20 looks strange as well - all you're wanting to do is increment a until primes is full, so you only need one loop for that. It's not clear at the moment how the inner loop is ever going to end.

Campbell's also right about the Sieve of Eratosthenes if you're looking for a good but simple way to generate primes (although in its simplest form that generates primes up to a particular value rather than a particular number of them).