We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,578 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Counting Prime Numbers in Java

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);
}
4
Contributors
4
Replies
18 Hours
Discussion Span
3 Years Ago
Last Updated
6
Views
spittballz
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 91
Skill Endorsements: 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?

apegram
LINQ!
Team Colleague
552 posts since Jan 2010
Reputation Points: 327
Solved Threads: 135
Skill Endorsements: 8

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.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 91
Skill Endorsements: 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 :)

jwenting
duckman
Team Colleague
8,558 posts since Nov 2004
Reputation Points: 1,674
Solved Threads: 347
Skill Endorsements: 19

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0640 seconds using 2.66MB