数のリストを読み取り、それらが素数かどうかを判断し、それらをファイルに書き込むプログラムを作成します。この質問を以前に投稿しました..数値が素数であるかどうかを判断するロジックを修正しました。「PrimeNumbers.txt」という名前のテキストファイルに書き込むことがわかりません
ある時点で 1 行を書き込むことができましたが、現在はテキスト ファイルに何も書き込まれません。お知らせ下さい。
import java.io.*;
import java.util.Scanner;
public class AssignFive_FileRead {
public static void main(String[] args) throws IOException {
int number;
int count = 0;
int calc = 0;
int i = 2;
File myFile = new File("assignment5Numbers.txt");
File myTargetFile = new File("PrimeNumbers.txt");
Scanner inputFile = new Scanner(myFile);
System.out.println("** Checking for required files **");
// Check to see if file's exists
if (!myTargetFile.exists()) {
System.out.println("Error: Unable to create target file!");
System.exit(0);
} else {
System.out.println("Target file has been created!");
}
if (!myFile.exists()) {
System.out.println("Error: file cannot be found");
System.exit(0);
} else {
System.out.println("Source file has been found, starting operation...");
System.out.println();
}
// Reading numbers from text file
while (inputFile.hasNext()) {
number = inputFile.nextInt();
while (i <= number / 2) {
if (number % i == 0) {
calc = 1;
}
i++;
} // End second while loop
if (calc != 1) {
count++;
for (int x = 0; x <= count; x++) {
PrintWriter outputFile = new PrintWriter("PrimeNumbers.txt");
outputFile.print(number + "\t");
outputFile.println("is prime");
}
}
// resetting variables for next check
calc = 0;
i = 2;
} // End first while loop
// System.out.println("Source file has a total of " + count + " numbers");
System.out.println("Data has been written to files.. Operation successful!");
} // End main
} // End public class