isPrime Help

Need help putting isPrime into JOptionPane

Page 1 of 1

5 Replies - 4781 Views - Last Post: 07 December 2009 - 08:34 PM Rate Topic: -----

#1 Drazzminius  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-December 09

isPrime Help

Post icon  Posted 07 December 2009 - 06:42 AM

Hello, first post. I'm using JCreator Lite for my Intro to Java Class (an online course since I'm stationed overseas in Japan) and I'm working on a homework assignment with the following requirement:

Quote

Write a Java program that meets the following requirements:
Declare a method to determine whether an integer is a prime number
Use the following method declarations: public static Boolean isPrime (int num)
An integer greater than 1 is a prime number if its only divisor is 1 or itself. For example, isPrime (11) returns true, and isPrime (9) returns false.
Us the isPrime method to find the first thousand prime numbers and display every ten prime numbers in a row, as follows:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 61 73 79 83 89 97
Important Notes: The input and output must use JOptionPane dialog and display boxes.
Here is what I have so far:
import javax.swing.JOptionPane;

public class PrimeNumber {
	// Main Method
	public static boolean isPrime(int num) {
		
		int[] numbers = new int[0];
		// Reads an input number
		for (int i = 0; i < numbers.length; i++) {
			String numString = JOptionPane.showInputDialog(null,
			"Enter a number: ",
			"Assignment 5.1 Input", JOptionPane.QUESTION_MESSAGE);
			
		// Convert string into integer
		numbers[i] = Integer.parseInt(numString);		
		}
		// Determines primes
		if (num <  2) return false;
		if (num == 2) return true;
		int n = (int) Math.sqrt(num);
		for (int i = 3; i < n; i += 2) {
			if (num%i == 0) return false;
		}
		return true;
	}
	public void main(String[] args) {
		//Declares "primes" as my variable
		int[] primes = new int[1000];	
		primes[0] = 2;				
		int count = 1;
		int num = 3;
		// Finds the first 1000 primes
		while (count<1000) {
			if (isPrime(num)) {
				primes[count] = num; 
				count++;
			}
			num = num + 2;
		}
		// Prepares the output
		String output = "The first 1000 primes are: ";
		for (int i=0; i<primes.length; i++)
		
		output += primes[i] + "\t";
		 		
			// Display the result
		JOptionPane.showMessageDialog(null, output, 
			"Assignment 5.1 Output", JOptionPane.INFORMATION_MESSAGE);
		
	System.exit(0);
	}
}
It shows as building properly, but before it runs I get this error:

Quote

--------------------Configuration: <Default>--------------------
java.lang.NoSuchMethodError: main
Exception in thread "main"
Process completed.


I'm not asking for someone to do my homework for me - just some help going in the right direction. What am I doing wrong? Everything else seems to be going okay and the examples I was given in my course seem to line up.

Any assistance is greatly appreciated.

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: isPrime Help

#2 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12222
  • View blog
  • Posts: 45,296
  • Joined: 27-December 08

Re: isPrime Help

Posted 07 December 2009 - 06:52 AM

Your main() method has to be a static method, meaning you have to include the word static. It always must be typed as: public static void main(String[] args). This says to Java to start the program, and if it isn't present as I typed above, you will get the error you got below.
Was This Post Helpful? 1
  • +
  • -

#3 Drazzminius  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-December 09

Re: isPrime Help

Posted 07 December 2009 - 07:03 AM

THANKS!

However, now I'm stuck in a never-ending loop for the input? I used the following example from my class to build my output:

Quote

import javax.swing.JOptionPane;

public class TestArray {
	//Main Method
	public static void main(String[] args) {
		int[] numbers = new int[6];
		
		// Read all numbers
		for (int i = 0; i < numbers.length; i++) {
			String numString = JOptionPane.showInputDialog(null,
			"Enter a number: ",
			"Example 5.1 Input", JOptionPane.QUESTION_MESSAGE);
			
		// Convert string into integer
		numbers[i] = Integer.parseInt(numString);		
		}
		
		// Find the largest
		int max = numbers[0];
		for (int i = 1; i < numbers.length; i++) {
			if (max < numbers[i])
				max = numbers[i];
		}
		// Find the occurrence of the largest number
		int count = 0;
		for (int i = 0; i < numbers.length; i++) {
			if (numbers[i] == max) count++;
		}
		
		// Prepare the result
		String output = "The array is ";
		for (int i = 0; i < numbers.length; i++) {
			output += numbers[i] + " ";
		}
		
		output += "\nThe largest number is " + max;
		output += "\nThe occurrence count of the largest number " 
			+ "is " + count;
			
		// Display the result
		JOptionPane.showMessageDialog(null, output, 
			"Example 5.1 Output", JOptionPane.INFORMATION_MESSAGE);
		
		System.exit(0);
	}
}

Not sure how to get it going with only a single entry (or preferably NO ENTRY, just a click box).

Thanks again for the quick reply!
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12222
  • View blog
  • Posts: 45,296
  • Joined: 27-December 08

Re: isPrime Help

Posted 07 December 2009 - 08:45 AM

Your input doesn't run an infinite loop. I ran it and it works just fine. To clarify, are you unsure how to recalculate your statistics and display on each new input?
Was This Post Helpful? 0
  • +
  • -

#5 Drazzminius  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-December 09

Re: isPrime Help

Posted 07 December 2009 - 01:03 PM

View Postmacosxnerd101, on 7 Dec, 2009 - 07:45 AM, said:

Your input doesn't run an infinite loop. I ran it and it works just fine. To clarify, are you unsure how to recalculate your statistics and display on each new input?

When I run it in JCreator, it gives me the input message box and keeps asking for inputs. If I hit cancel or enter nothing, the program aborts.

My intent is to get the program to input a number and then show a message box listing all Primes up to 10000 in rows of 10.
Was This Post Helpful? 0
  • +
  • -

#6 Drazzminius  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-December 09

Re: isPrime Help

Posted 07 December 2009 - 08:34 PM

Here is the latest version. I just can't seem to get the columns to work right.

import javax.swing.JOptionPane;

public class PrimeNumberTest {	
		
		// Check if an integer is a prime
	public static boolean isPrime(int num) {
		if (num <  2) return false;
		if (num == 2) return true;
		int n = (int) Math.sqrt(num);
		for (int i=3; i<n; i+=2) {
			if (num%i == 0) return false;
		}
		return true;
	}
	// Main Method
	public static void main(String[] args) {
		int[] primes = new int[1000];	// Define the array of the first 1000 primes
		primes[0] = 2;					// The smallest prime is 2;
		int count = 1;
		int num = 3;
		// Find the first 1000 primes
		while (count<1000) {
			if (isPrime(num)) {
				primes[count] = num; 
				count++;
			}
			num = num + 2;
		}
		// Opens the Welcome Message
		JOptionPane.showMessageDialog(null, "Click OK to see all prime numbers under 10000", 
		"Assignment 5.1", JOptionPane.INFORMATION_MESSAGE);
		
		// Prepares the output
		String output = "The first 1000 primes are: ";
		for (int i=0; i<primes.length; i++)	{
		//	if (i%10 == 0), primes[i] ("");
			output += primes[i] + "\n";}
		 		
			// Display the result
		JOptionPane.showMessageDialog(null, output, 
			"Assignment 5.1 Output", JOptionPane.INFORMATION_MESSAGE);
		
		System.exit(0);
	}
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1