| | |
Errors writing to file
![]() |
•
•
Join Date: May 2008
Posts: 6
Reputation:
Solved Threads: 0
I'm trying to write a file that first decides if a number entered is prime or not, then if it is writes it to a file. Here's what I've got, but I keep coming up with a couple errors that I can't figure out how to fix. One is that I can't figure out how to bring the number into the second method (which I have to use). I'm also not sure I'm using the write way to write to file. Help?
import java.io.*;
import. java.util.Scanner;
int num;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a positive integer: ");
num = keyboard.nextInt();
for (int x=2; x < num; x++)
{
if (num%x==0)
noPrime();
else
prime();
}
System.exit(0);
}
public static void prime(int num)
{
System.out.println(num + " is prime.");
while (num <=100 && num >=1);
{
//Open the file.
PrintWriter outputFile = new PrintWriter(PrimeNumbers.txt);
//Write number to the file.
outputFile.println(PrimeNumbers.txt);
//Close the file.
outputFile.close();
System.out.println("Numbers written to file.");
}
}
public static void noPrime(int num)
{
System.out.println(num + " is not prime.");
}
}//end class
import java.io.*;
import. java.util.Scanner;
int num;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a positive integer: ");
num = keyboard.nextInt();
for (int x=2; x < num; x++)
{
if (num%x==0)
noPrime();
else
prime();
}
System.exit(0);
}
public static void prime(int num)
{
System.out.println(num + " is prime.");
while (num <=100 && num >=1);
{
//Open the file.
PrintWriter outputFile = new PrintWriter(PrimeNumbers.txt);
//Write number to the file.
outputFile.println(PrimeNumbers.txt);
//Close the file.
outputFile.close();
System.out.println("Numbers written to file.");
}
}
public static void noPrime(int num)
{
System.out.println(num + " is not prime.");
}
}//end class
Can't you read the errors and follow the instructions to fix them? When you get compile errors you get a description of the error, the line that it happened and how to fix it.
Why doesn't anybody read compile errors these days and just post the code without even bothering to see what the compiler says??
And most important, you didn't even post these errors, you just gave us the code. Are we suppose to compile it for you in order to find out the errors?
Anyway, you declare prime and noPrime like this:
public static void noPrime(int num)
public static void prime(int num)
and you use them like this:
noPrime();
prime();
You figure it out.
Why doesn't anybody read compile errors these days and just post the code without even bothering to see what the compiler says??
And most important, you didn't even post these errors, you just gave us the code. Are we suppose to compile it for you in order to find out the errors?
Anyway, you declare prime and noPrime like this:
public static void noPrime(int num)
public static void prime(int num)
and you use them like this:
noPrime();
prime();
You figure it out.
Check out my New Bike at my Public Profile at the "About Me" tab
Also I think that you are missing the main method and this piece of code:
is not inside a method, it's in the middle of the class
Java Syntax (Toggle Plain Text)
Scanner keyboard = new Scanner(System.in); System.out.println("Please enter a positive integer: "); num = keyboard.nextInt(); for (int x=2; x < num; x++) { if (num%x==0) noPrime(); else prime(); } System.exit(0); }
is not inside a method, it's in the middle of the class
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- Copy from one file to another (C++)
- Writing to a file (C++)
- Problems writing stdout to a file, please help! (Perl)
- Custom Class - File I/O (C++)
- Getting errors while compiling (Java)
- What errors can occur while reading a file? (C)
- Error trying to write to existing file (C)
- Errors writing a vector to a file (C++)
- Read and writing strings from structures (C++)
Other Threads in the Java Forum
- Previous Thread: binding properties
- Next Thread: Process interaction with Java?
| Thread Tools | Search this Thread |
-xlint 911 actionlistener addball addressbook android applet application array automation binary blackberry block bluetooth button character class client code compile component consumer css csv database desktop developmenthelp eclipse ee error fractal ftp game gameprogramming givemetehcodez graphics gui html image j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia jvm lego linked linux loan mac method mobile netbeans notdisplaying number objects online oriented phone printf problem program programming project projects recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set singleton sms software sort sql string swing system test textfields threads time title tree tutorial-sample ubuntu update windows






