•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 361,570 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,043 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 166 | Replies: 2
![]() |
•
•
Join Date: May 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
Last edited by ebebcb : May 14th, 2008 at 11:19 pm.
.,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!!!
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!!!
Last edited by alpe gulay : May 15th, 2008 at 1:42 am.
what you are today!
it's because on what you did yesterday'
what you did today!
is what in future you will be...'
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
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
I AM the 12th CYLON
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
Similar Threads
- fstream Tutorial (C++)
- Initialising (C++)
- postfix expression evaluation (C++)
- C Programmers Help ! (C)
- Can JAVA create DOS directories? (Java)
- fstream problems. No output to file. (C++)
- turning userinput into a text file (C)
- can't link to included files... y? (C++)
- storing references to classes (C++)
Other Threads in the Java Forum
- Previous Thread: Don't think I'm getting "count" correct
- Next Thread: can you see whtas wrong with this one!!


Linear Mode