• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Program that takes an int and returns an array of all the prime numbers smaller than that int

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure what is wrong with my code

Basically, for whatever int I put in, I get the biggest prime number (that is smaller than that int), print the amount of times that there are prime numbers in the array.

I very much appreciate anyone who can help me out.

 
Ranch Hand
Posts: 33
VI Editor Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem you have is that, when you put the primes into the array, that you, for each of the array indexes keep walking through every value and keep overwriting the previously found one
=> ending up with an array where each index contains the maximum prime below the value entered.




I strongly suggest you have a look at ArrayList.

This way you can add every Integer (or int via autoboxing) you come across to this arrayList instead of having to go through them for the size first and then again for adding them.

If you change your method to return a List of integers, you can also directly loop through all values of the List with an enhanced for loop with autounboxing.



Regards,

Stefaan
 
mark donner
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. Very helpful
reply
    Bookmark Topic Watch Topic
  • New Topic