Results 1 to 4 of 4

Thread: Prime Number Help

Threaded View

  1. #2

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Location
    Okinawa, Japan
    Posts
    6

    Re: Prime Number Help

    I've made the following updates:
    Code:
    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&#37;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:
    --------------------Configuration: <Default>--------------------
    java.lang.NoSuchMethodError: main
    Exception in thread "main"
    Process completed.
    ???
    Last edited by Drazzminius; Dec 7th, 2009 at 07:54 AM. Reason: Updated coding

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width