• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java Prime Number help

wolf2009

[H]ard|Gawd
Joined
Jan 23, 2008
Messages
1,767
I need help with making this prime number generator in java for univ HW. I'm using BlueJ for writing and testing the code.

I have made the code, but some logic is wrong. The user has to enter the starting number, 2 for HW, and the ending number. Then the code should display all the prime numbers between the two numbers. And it has to be done with loops and if-else statements.

Code:
import java.util.Scanner;
public class PrimeNumber
{
    public static void main(String [] args)
    {
        int num1, num2, count, i = 2, prChk = 0 , m = 0;
        Scanner keyIn = new Scanner (System.in);
    
      
        System.out.print("Enter First number : ");
        num1 = keyIn.nextInt();
        
        System.out.print("Enter Second number : ");
        num2 = keyIn.nextInt();
        
        count = num1;
              
        while(count <= num2)
        {
            while(i < count)
            {
                prChk = count % i;
                if(prChk == 0)
                    m = 0;
                else if(prChk == 1)
                    m = 1;
                i++;
            }
            if (m == 0)
                System.out.println(count + ": prime");
            else if(m ==1)
                System.out.println(count + ": not prime");
            count++;
        }
    }
}

What am I doing wrong ?
 
What's happening in the code that makes you think it's wrong? What's it outputting?

Also, you never reinitialize i.
 
launch your program in a debugger; you'll quickly see where you're going wrong.
 
Is eclipse better than what sun offers on their site, netbeans?

It's debatable, and some people get their panties in a bunch over it...

It's like emacs vs. vi, Mac vs. Windows, etc.. mainly preference.

Netbeans is very good, and I like it because of the swing GUI tools (helps with quick GUI prototyping).

I personally use Eclipse more though, I just find it more extensible and flexible for my needs. (Plus I can run it from a flash drive without it wigging out on me)
 
Is eclipse better than what sun offers on their site, netbeans?

Having used both I find Eclipse is more flexible when it comes to working with Java EE, although that can depend on implementations (i.e. Glassfish). For straight Java SE code its pretty much a dead heat however if your making heavy use of Swing, Netbeans takes an advantage if you like visual coding.

I also find Eclipse is a lot easier to use when you are working on a team with other people who use different (or don't use) IDEs.

It really does boil down to Vim vs. Emacs type debate but I find that one or the other is usually better at accomplishing some goal so it really depends on the project/module your working on.
 
What's happening in the code that makes you think it's wrong? What's it outputting?

+ 1 to this question. I'm running the code now. Doesn't seem to be anything wrong to me. Unless there's a set of criteria I'm missing?
 
Back
Top