This week's book giveaway is in the Cloud/Virtualization forum.We're giving away four copies of Pipeline as Code and have Mohamed Labouardy on-line!See this thread for details.
Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Rob Spoor
• Henry Wong
• Liutauras Vilda
Saloon Keepers:
• Tim Moores
• Carey Brown
• Stephan van Hulst
• Tim Holloway
• Piet Souris
Bartenders:
• Frits Walraven
• Himai Minh
• Jj Roberts

# Trying to print first 100 prime numbers using an array

Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:
Hi everybody. I'm very new to Java as I'm sure you'll be able to tell after you read this post. I am trying to print the first 100 prime numbers using an array. The flowchart I'm using is below followed by the code I have right now. There are many things I'm having a hard time figuring out.

1. How do I give the first slot in the array a value of 1? I thought I did that but it doesn't work.
2. If prime = false how do I tell the array to skip that slot instead of entering a value of 0?
3. Am I doing any of this correctly?

Flow Chart

Initialize array

Set array to size of 100

Set candidate and first array slot = 1

Do

Get candidate += 2

For every value in array

Set prime as true

If candidate % any array value = 0

Y- Set prime as false

N - Enter candidate in array
Print candidate

While array <= 99

Code

Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:

1.) You did it correctly, Array[0] is set to 1.

Additional info: When creating a new array like this

all "slots" [0] to [99] are initialized with 0 per default. Therefore you get an ArithmeticException (devision by zerÃ³) at

lowercase baba
Posts: 12974
66
• 1
• Number of slices to send:
Optional 'thank-you' note:
If you create an array of primitives, they will all be initially set to 0. There are a couple of approaches you could take...

1) Keep track of how many primes you have found, and then only check up to that position in the array (an addition condition in your do-while loop, maybe?)
2) Initialize every element of your array to something like "-1". use this as a sentinel and only do the division when the value is NOT equal to -1
3) Don't use an array. You can use an ArrayList, which will grow as you need it to do so. You will probably have to use Integers instead of ints, but that shouldn't be a major change

Also...remember that 1 is NOT prime, and 2 IS prime.

Johannes Uhlmann
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:
If you are are interested in a solution to your problem, here it is:

Ranch Hand
Posts: 96
• Number of slices to send:
Optional 'thank-you' note:
@Johannes: I'm fairly new to the forum too but I believe that the idea behind the forum is not "you've got a java problem? here is the code that solves it".
It's more like "learn a man to fish and he's got food for the rest of his life".

Hence, try to avoid dropping 'solution code' like that in your answer. The goal is to help the original poster gain insight in Java and learn good practices. See for example the suggestion/advice from fred (ArrayList instead of array etc).

Cheers,
Wim

fred rosenberger
lowercase baba
Posts: 12974
66
• Number of slices to send:
Optional 'thank-you' note:
Wim is correct. We not only don't want people to provide solutions, we actively delete them (as I have done). Our goal is to teach people to be programmers, and providing an answer does NOT do that.

Greenhorn
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
You have 2 loops in your code. You sure you want that?

You have have your value of i used in both loop controls.

Johannes Uhlmann
Greenhorn
Posts: 5
• Number of slices to send:
Optional 'thank-you' note:

fred rosenberger wrote:Wim is correct. We not only don't want people to provide solutions, we actively delete them (as I have done). Our goal is to teach people to be programmers, and providing an answer does NOT do that.

I agree, sorry.

fred rosenberger
lowercase baba
Posts: 12974
66
• Number of slices to send:
Optional 'thank-you' note:
Not a problem. We're glad you're here to help. You may want to skim our HowToAskQuestionsOnJavaRanch and HowToAnswerQuestionsOnJavaRanch FAQs (those should both link to the proper page).

M Roberts
Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:

Johannes Ebbighausen wrote:

all "slots" [0] to [99] are initialized with 0 per default. Therefore you get an ArithmeticException (devision by zerÃ³) at

That is extremely helpful and also makes me realize this is going to be even more difficult than I thought.

I guess I'll try to use an ArrayList. It sounds like that's closer to what I am trying to accomplish.

M Roberts
Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:

Joseph Kampf wrote:You have 2 loops in your code. You sure you want that?

You have have your value of i used in both loop controls.

If I don't use a second loop that includes i how can I print all the values in the array?

M Roberts
Greenhorn
Posts: 4
• Number of slices to send:
Optional 'thank-you' note:
Thanks to the people that tried to help but after many hours last night I wasn't able to figure it out. I'll get the answer and not many homework points from the teacher later today. I will be so so glad when this class is over.

 Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad: SKIP - a book about connecting industrious people with elderly land owners https://coderanch.com/t/skip-book