I'm trying to write a program that first decides if a number is prime, and if it is writes it to a file I created. I'm obviously having problems (or I wouldn't be pulling my hair out and posting this). First, the errors say an identifier expected after the "public static void prime(isPrime, int number)" and "public static void noPrime(isPrime, int number)" lines. After that, I'm sure it's all messed up. Any help? Thanks.

import java.util.Scanner;
import java.io.*;

public class primeNumberToFile
{
   public static void main (String[ ] args) 
   {

        //variables
        int num; 

        //Call methods in main
        prime(isPrime);
        noPrime(isPrime);

   }//end class

    // Determine whether it is prime or not.
   public static boolean isPrime (int number)   
   {
            //Create Scanner object for keyboard
            Scanner keyboard = new Scanner(System.in);

            //Get integer from user.
            System.out.println("Please enter a positive integer: ");

            number = keyboard.nextInt();

            boolean isPrime;
            for (int x=2; x < number; x++)

        {
            if (number%x<0)
                isPrime = true;
            else
                isPrime = false;    

        }
            return isPrime;

   }//end class

    //If the number is prime, tell that to user, the write that number to a file.
    public static void prime(isPrime, int number) 
                            throws IOException
    {
        int number;

            System.out.println(number + " is prime.");

                //Open the file.
                PrintWriter outputFile = new PrintWriter("PrimeNumbers.txt");

                while (number <=100 && number >=1);
            {

                //Write number to the file.
                outputFile.println("PrimeNumbers.txt");

                //Close the file.
                outputFile.close();
                System.out.println("Numbers written to file.");
            }


    }//end class

    //if the number is not prime, tell the user that.
    public static void noPrime(isPrime, int number)
    {
        System.out.println(number + " is not prime.");
    }//end class


}//end main

Recommended Answers

All 2 Replies

maybe you can try this one...
take a look with this example.,

int input=5;
if(input%2!=0)
{
System.out.print(input+" is prime number!");
//put the data this in your txt file...
}
else
{
    System.out.print(input+ " is not prime number!");
}

that condition above will tell if the number is prime or not..
tell me if this one works in your code ok!!!

You should post the errors the compiler gives you, and at which line you get them.
You call these methods like this:

prime(isPrime);
noPrime(isPrime);

And you declare them like this:

public static void prime(isPrime, int number)
public static void noPrime(isPrime, int number)

Plus, at the main method you don't declare the isPrime variable that you put inside the methods

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.