prime number problem

Hi, I am new to java and and I am trying to figure out what my problem is with this prime number program. I have looked through other posts, but I was wondering if someone could look over my specific code and give me some direction. I think I am close, but not sure how to get around my problem. I know I should use a boolean, but I am not sure why this isn't working. Right now, if I enter '4' for instance, the message says it is a prime number. Any help?

Thanks.

here is the code I have so far:

public static void main(String[] args) {

int entry = Integer.parseInt(JOptionPane.showInputDialog("Please enter a number greater than 1:"));

boolean isPrime = true;

if (entry == 0){

System.exit(0);

}

for (int i = entry - 1; i > 1; i--){

int mod = entry%i;

if(mod == 0){

//JOptionPane.showMessageDialog(null, "The number " + entry + " is composite.");

isPrime = false;

break;

}

if (isPrime == true)

JOptionPane.showMessageDialog(null, "The number " + entry + " is prime.");

if (isPrime == false)

JOptionPane.showMessageDialog(null, "The number " + entry + " is composite.");

}

}

}

[1222 byte] By [kalugaa] at [2007-11-26 18:13:56]
# 1
First of all, check the number to see if it is greater than 1.Second, in the loop, start with i being 2 and keep adding one until it is greater than the input's square root, rounded up.
Lord_Jirachia at 2007-7-9 5:47:12 > top of Java-index,Java Essentials,New To Java...
# 2
You're showing the message dialogs in a loop. With 4, if should tell you that it is prime, then tell you it isn't, then tell you it isn't again. Put the message dialogs outside your for loop.
CaptainMorgan08a at 2007-7-9 5:47:12 > top of Java-index,Java Essentials,New To Java...
# 3

It's very hard to read your code. When you post code, please use[code] and [/code] tags as described in [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.

It looks like you're displaying prime/not prime every time through the loop. You should be doing it just once, after the loop completes.

Also, the ==true and ==false are bad form.

if (isPrime) {

// display prime message

}

else {

// display not prime

}

And always use { } for if, for, etc., even when you don't strictly need them.

jverda at 2007-7-9 5:47:12 > top of Java-index,Java Essentials,New To Java...
# 4
cool, thanks so much for your tips, both on syntax and logic
kalugaa at 2007-7-9 5:47:12 > top of Java-index,Java Essentials,New To Java...