# Thread: First attempt at Java.

1. Member
Join Date
Nov 2010
Posts
3
Rep Power
0

## First attempt at Java.

Hello guys! I've only started learning Java in the past couple weeks and have no real programming experience. I've been reading a book called Big Java which is helping me along really well. Anyhow I just wrote my first program which is basically just a little bit of code that finds prime numbers between 1 and n.

I managed to get it to do what I wanted it to, I was just wanting to know whether people could give me any advice on whether this is good coding layout and whether it's as efficient as it can be. Just any criticism is welcome. Thanks!

Java Code:
```public class PrimeNumberFinderInt
{
public static void main(String[] args)
{
int currentNumber = 1;
int divisibleBy = 2;
for(int currentNumberx = currentNumber; currentNumberx < 30; currentNumberx += 1)
{
for(int divisibleByFor = divisibleBy; divisibleByFor < currentNumberx; divisibleByFor += 1)
{
int a = currentNumberx%divisibleByFor;
if(a > 0 && divisibleByFor < currentNumberx - 1)
{
continue;
}
else if(a == 0)
{
System.out.println("The number " + currentNumberx + " was divided by " + divisibleByFor);
break;
}
else
{
System.out.println(currentNumberx + " is a prime number!");
}
}
}
}
}```

2. I personally find that if( ... ) else if( ... ) else construct very cryptic. Why not make a boolean method isPrime( ... ) that returns true if the parameter passed in is a prime number and false otherwise. Also note that if a number has divisors a lot of them are smaller or equal than the root of that number; in other words: if it has a divisor larger than the root of the number it also has a divisor smaller or equal than that root; consequently you only have to search for numbers smaller or equal to that root. Also also note that all primes (except 2 and 3) are multiples of 6 plus or minus one.

kind regards,

Jos

3. Member
Join Date
Nov 2010
Posts
3
Rep Power
0
Originally Posted by JosAH
I personally find that if( ... ) else if( ... ) else construct very cryptic. Why not make a boolean method isPrime( ... ) that returns true if the parameter passed in is a prime number and false otherwise. Also note that if a number has divisors a lot of them are smaller or equal than the root of that number; in other words: if it has a divisor larger than the root of the number it also has a divisor smaller or equal than that root; consequently you only have to search for numbers smaller or equal to that root. Also also note that all primes (except 2 and 3) are multiples of 6 plus or minus one.

kind regards,

Jos
Thanks for the constructive advice. I will try to use the boolean method you suggested. I'm also not sure whether I'm using break or continue correctly and so I kind of just threw them in through a trial and error process.

4. Member
Join Date
Oct 2010
Posts
3
Rep Power
0
could set currentNumber as final too, as its only declared once anyway.

5. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
5
wonders how the original code actually compiled

6. Member
Join Date
Nov 2010
Posts
10
Rep Power
0
Your code is pretty dang good for a beginner. All i ever used to use were if's ha ha.

If i were you, i would take lots of notes. it saves a lot of time down the road when you forget what a method does and have to go back and re-read through it. It only adds unneeded frustration.

good job =)

edit:

just remember the continue statement just stops the loop at where the continue was stated, and starts the loop over again. The break statement kills the loop entirely. trial and error is your best teacher with java programming =) its fun to go on adventures.
Last edited by cejay; 11-27-2010 at 09:57 AM.

7. Member
Join Date
Nov 2010
Posts
3
Rep Power
0
Originally Posted by cejay
Your code is pretty dang good for a beginner. All i ever used to use were if's ha ha.

If i were you, i would take lots of notes. it saves a lot of time down the road when you forget what a method does and have to go back and re-read through it. It only adds unneeded frustration.

good job =)

edit:

just remember the continue statement just stops the loop at where the continue was stated, and starts the loop over again. The break statement kills the loop entirely. trial and error is your best teacher with java programming =) its fun to go on adventures.
I wasn't sure whether putting the continue and break within the if and else checks would mean the the FOR loop is effected by the continue and break. I obviously don't want it to keep checking for numbers if it's already found one it's divisible by.

This book has been trying to drill me to put comments in but I figure for tests such as this which I am highly unlikely to use again then there's no real point to it.