1.11M Members

Counting Prime Numbers in Java

 
0
 

I am trying to create a program that counts the Prime numbers then I will continue and use this second method in my main method.
I am confused how to count the Prime numbers. I just pulled the text from JEdit.. not sure if that was the right way to enter my text.

public class Prime
{
	private static boolean primeCounter(int number)
	{
		int counter = 0
		
		for (int i = 2; i < number; i++)
	        {
		if (number % i == 0)
			return false;
		else
			return true;

		if (false);  //not sure how to count which prime number I am up to according to my user input of a positive integer
		counter++;
	        }
	}

// This is where I will enter my main method
                public static void main(String[] args)
	{

		String userInput = JOptionPane.showInputDialog(null, "Please enter a positive Integer:");
		int num = Integer.parseInt(userInput);
}
 
0
 

You have made a lot of syntactical mistakes here.

if (number % i == 0)
	return false;
else
	return true;
if (false);

Do you really think the control will ever reach the line "if (false)" ? Also let's for a moment assume it does, will the control ever enter the statement in the if block ? What do you think are you doing evaluating if (false) ?
By putting a semi-colon immediately after the if condition you are terminating the if block there itself, so the "counter++" statement falls outside the block and is executed irrelevant of the condition in the if block being true or false.

Instead of return false/ return true you should set a flag say NOT_PRIME or COMPOSITE to true when the number is divisible and then exit the loop without checking further using the break keyword.

 
0
 

As another suggestion, what sense does it make to try to divide 11212 by 11211? That's what would happen as a result of your loop

for (int i = 2; i < number; i++)

Think about what would actually be a good lesser-number to test through. When could you be assured you have accounted for all possible divisors? (And in answering this, consider that when you divide something by one number, you have also accounted for the result as a divisor.)

In general, though, I'm a bit confused as to what the purpose of your program is. Is your intent to count all prime numbers between 2 and the value supplied by the parameter? If so, why are you returning a boolean? Is it your intention to actually return the list of all prime numbers within the given range? Or is it simply to test whether the given number is a prime number? If so, why is there a counter?

 
0
 

Yes apegram has correctly pointed out that you don't seem to have the logic cut out well, do the dry coding make the necessary changes and post again. Elementary mistakes have already been pointed out so correct them as well.

 
0
 

You have made a lot of syntactical mistakes here.

if (number % i == 0)
	return false;
else
	return true;
if (false);

Do you really think the control will ever reach the line "if (false)" ? Also let's for a moment assume it does, will the control ever enter the statement in the if block ? What do you think are you doing evaluating if (false) ?

Not to mention the complete waste of code there.
Simply writing

return (number % i == 0);

would work just as well :)

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article