User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: May 2008
Posts: 1
Reputation: ebebcb is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ebebcb ebebcb is offline Offline
Newbie Poster

Problem with methods and writing to file

  #1  
May 14th, 2008
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
Last edited by ebebcb : May 14th, 2008 at 11:19 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Posts: 35
Reputation: alpe gulay is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
alpe gulay's Avatar
alpe gulay alpe gulay is offline Offline
Light Poster

Re: Problem with methods and writing to file

  #2  
May 15th, 2008
.,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!!!
Last edited by alpe gulay : May 15th, 2008 at 1:42 am.
.,'Baby Pro'.,
what you are today!
it's because on what you did yesterday'
what you did today!
is what in future you will be...'
Reply With Quote  
Join Date: Dec 2007
Location: Greece
Posts: 353
Reputation: javaAddict is on a distinguished road 
Rep Power: 1
Solved Threads: 33
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Whiz

Re: Problem with methods and writing to file

  #3  
May 15th, 2008
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
I AM the 12th CYLON
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Java Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 3:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC