Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JOptionPane Prime Number Display

843806Aug 1 2008 — edited Aug 1 2008
I'm writing this program that determines whether an integer is a prime number using the isPrime method. It's supposed find the first thousand prime numbers and display every ten prime numbers in a row. However, I'm having problems displaying it in a JOptionPane dialog box. Currently It shows up number by number in sequential dialog boxes, not how I intend it. Can anyone help me identify the problem?
// PrimeFinder.java
import javax.swing.JOptionPane; 

public class PrimeFinder
{
   // 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;
   } // end boolean isPrime
	
   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;
      }
      
      for (int i=0; i<primes.length; i++)
      {
         if (i%10 == 0)
         JOptionPane.showMessageDialog(null, "" + primes[i] + "\n", 
            "Prime Numbers Output", JOptionPane.INFORMATION_MESSAGE); 		
      }
		
		System.exit(0);
   } // end main method
} // end class PrimeFinder
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 29 2008
Added on Aug 1 2008
1 comment
904 views