A prime number is a number that is only evenly divisible by itself and 1. For example, the number 5 is prime because it can only be evenly divided by 1 and 5. The number 6, however, is not prime because it can be divided evenly by 1, 2, 3, and 6.
Write a method named isPrime, which takes an integer as an argument and returns true if the argument is a prime number, or false otherwise. Demonstrate the method in a complete program.
This is what I have so far.
import javax.swing.JOptionPane;
public class PrimeChecker
{
public static void main(String[] args)
{
String input; // To hold keyboard input
String message; // Message to display
int number; // Number to check for prime
// Get the number.
input = JOptionPane.showInputDialog("Enter a number.");
number = Integer.parseInt(input);
// Determine whether it is prime or not.
if (isPrime(number))
message = "That is a prime number.";
else
message = "That is not a prime number.";
// Display a message.
JOptionPane.showMessageDialog(null, message);
System.exit(0);
}
}