| | |
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 |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






