Member Avatar for jonoj

Hi everyone
I am new to java and am writing a program thats meant to have the following terminal output...
Enter a number: 29
29 is a prime number.
Enter a number: -7
Please enter a positive integer.
Enter a number: 45
45 is not a prime number.
Enter a number: 0
Exiting program...

I think there is meant to be four main structures to the program -
1. User enters number.
2. if number less than 0 then print "enter positive" and go back to 1
3. if number over 0 do following calculation - for(i=2; i <= number;
i--){ if number / i ==0 print not prime. else print prime
4. 1,2,3 keep repeating till 0 is pressed

I am not sure how to implement this into my program, linking it
together with the correct loops.

here is my current source code:

import java.io.*;

class PrimeChecker
{

    public static void main(String[] args) throws IOException
    {

    BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));

    int number = 1;   

    System.out.print("Enter a positive integer number: ");
    number = Integer.parseInt(stdin.readLine());

    do
    {

        if(number <= 0)
        {
        System.out.println("please enter a positive integer.");
        }

        else if(number > 0)
        {
        for(int i = 2; i <= number; i--)       
        {

            if((number % i) == 0)
            {
            System.out.println("" + number + " is not a prime number");
            }

                else 
            {
            System.out.println("" + number + " is a prime number");
            }                   
        }
        }
    }
    while(number != 0);   

    }
    }

Thanks for any help
Jonathan

Im not sure that a do-while loop is the best way to go about this, might be though. Right now I dont have time to fix it but I can a little later.

Member Avatar for jonoj

ok thats cool

I believe this is something like what you are asking for..Let me know if it's not.

import java.io.*;

class PrimeChecker
{

 public static void main(String[] args) throws IOException
 {

   BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));
   String inData;
   int number = 0; 
   int count = 0;
  try {
   do {
       System.out.print("Enter a positive integer number: ");
       inData = stdin.readLine();
       number = Integer.parseInt(inData.trim());
     
       if (number > 0 && (number % 2) != 0)
       {
		System.out.println("The number is prime\n");
       }

       else if (number == 0)
       {
	System.out.println("Please enter a number larger than 0\n");
       }
	else 
 	 System.out.println("The number is not prime\n");
	count++;
    }
  

    while (!inData.equals("//exit"));
  }
  catch (NumberFormatException e) {}
	System.out.println("Exiting. Please wait....");
  
  
 }
}
Member Avatar for jonoj

Hi server
Thanks for that.
Pretty similair to my final submitted assignment except I used a for loop for the prime claculation, the way you did the calculation is clever - if a number over 0 divided by 2 has a remiender 0 its not a prime else is a prime. I also had to compensate for 1... which is not a prime.
Thanks again
Jonathan

(btw how do I make the wrap text to code button work?)
hers my final code just for interest... :mrgreen:

 /*
Assignment 2: Prime Checker
Written by Jonathan Fordyce - 75770
Started on 04/10/2004, completed on 10/10/2004
This program accepts a number input from the keyboard then if the number is over 0 it calculates wether or not the number is a prime, printing the answer, and prompting for user to enter another number. If the number is negative the program will keep repeating prompting for positive, if positive it will calculate and repeat prompt, and if input is zero the program will quit. 
*/

import java.io.*;

class PrimeChecker
{

    public static void main(String[] args) throws IOException
    {

    //creates keyboard input stream and defines variables
    BufferedReader stdin = new BufferedReader(new InputStreamReader (System.in));

    long number;  
    boolean prime;

    //prints message and quit intructions
    System.out.println("(Welcome to Prime Checker --- Press 0 to quit the program)");

    //repeat the following while number input isn't 0 (quit)
    do
    {
        //prompts user to enter integer number, and assigns variable as input
        System.out.print("Enter a positive integer: ");     
        number = Long.parseLong(stdin.readLine());

        //if input number is 0 quit program
        if(number == 0)
        {
        System.out.println("Exiting Prime Checker...");
        return;
        }     

        //if input number is over 1 do following calculations
        if(number > 1)
        {
        //assigns boolean prime as true
        prime = true;

        //creates divisor i which is numbers between 2 and number 
        for(long i = 2; i < number; i++)
        {
            //if the reminder of input number divided by i equals 0 
            if((number % i) == 0)       
            {    
            //print 'not a prime' and make boolean prime as false
            System.out.println(number + " is not a prime number.");
            prime = false;
            break;              
            }
        }   
        //if prime is true print 'is a prime'
        if(prime)
        {
            System.out.println(number + " is a prime number.");
        }               
        }

        //else if input number is below 0 print 'enter positive'
        else if(number < 0)
        {       
        System.out.println("Please enter a positive integer!");
        }

        //else if input number is below 2 print 'not a prime' 
        else if(number < 2)
        {
        System.out.println(number + " is not a prime number.");
        }
    }
    while(number != 0);
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.