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