Need assistance in writing a program to

gkilgore
gkilgore used Ask the Experts™
on
I have attempted to write a program which takes an integer as an argument adn returns true if the argument is a prime number or false otherwise.  I have to demonstrate the method in a complete program.   Unfortunately, my program is not producing true or false based on whether the number is prime or not.  Not sure what direction to go, in order to obtain the correct output.  Any assistance will greatly be appreciated.
import java.io.*;
 
public class PrimeNumber1
 {
	public static void main(String[] args) throws Exception
	{
		
			int TRUE = 1;
		int FALSE = 0;
 
 
     // Local variable declarations
   int Number = 0;
   int Prime = TRUE;
 
		
			BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter number:");
		int number = Integer.parseInt(bf.readLine());
	
					
		if (Number < 1 && Number >= 50)
      {
      System.out.println("Error: Number is out of range\n");
      }
 
   // Check if Number is prime
  		  if (Number == 1) Prime = FALSE;
 		  if ((Number > 2) && (Number % 2 == 0)) Prime = FALSE;
 		  if ((Number > 3) && (Number % 3 == 0)) Prime = FALSE;
 		  if ((Number > 5) && (Number % 5 == 0)) Prime = FALSE;
 		  if ((Number > 7) && (Number % 7 == 0)) Prime = FALSE;
		  if ((Number > 9) && (Number % 9 == 0)) Prime = FALSE;
 
   // Print output
   if (Prime == TRUE)
	{
     System.out.println("Number IS prime: True " + number);
	 } 
   else
	{
    System.out.println("Number is NOT prime: False " + number);
    }
}
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Java actually does have native booleans. You should probably use them instead of your virtual booleans using integers.

http://leepoint.net/notes-java/data/basic_types/26boolean.html

Also a variable name such as your "Number" should start with a lower caser letter.

Your real problem is that you have TWO number variables "Number" and "number". Java is case sensitive so these are two unique variables. "Number" is always "0", which according to your program is prime. Here is your code fixed.
import java.io.*;
 
public class PrimeNumber1
 {
	public static void main(String[] args) throws Exception
	{
		// Local variable declarations
		int number = 0;
		boolean prime = true;
 
 
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter number:");
		number = Integer.parseInt(bf.readLine());
 
 
		if (number < 1 && number >= 50)
      {
      System.out.println("Error: number is out of range\n");
      }
 
   // Check if number is prime
  		  if (number == 1) prime = false;
 		  if ((number > 2) && (number % 2 == 0)) prime = false;
 		  if ((number > 3) && (number % 3 == 0)) prime = false;
 		  if ((number > 5) && (number % 5 == 0)) prime = false;
 		  if ((number > 7) && (number % 7 == 0)) prime = false;
		  if ((number > 9) && (number % 9 == 0)) prime = false;
 
   // Print output
	if (prime == true)
	{
		System.out.println("Number IS prime: True " + number);
	}
	else
	{
		System.out.println("Number is NOT prime: False " + number);
	}
}
}

Open in new window

Author

Commented:
Thank you so very much for your prompt and accurate response.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial